Basic example of creating AWS EC2 with Terraform
Here the very basic example. This is just a note of creating single EC2 instance using Terraform.
First you need to define the provider in the example.tf file
provider "aws" {
region = "ap-southeast-2"
}
# resource "aws_s3_bucket" "example" {
# bucket = "ojithatest1"
# acl = "private"
# }
resource "aws_instance" "example" {
ami = "${lookup(var.amis, "ubuntu-server")}"
instance_type = "t2.micro"
# depends_on = ["aws_s3_bucket.example"]
provisioner "local-exec" {
command = "echo ${aws_instance.example.public_ip} > ip_address.txt"
}
}
resource "aws_eip" "ip" {
instance = "${aws_instance.example.id}"
}
If you need S3 bucket depends on that EC2 uncomment the above code. In the above code, we are just creating EC2 instance and the assciated Elastic IP address.
Above code use the variables such as amis
(input variable) and ip
(output variable) which are defined in the variables.tf file
variable "amis" {
type = "map"
}
output "ip" {
value = "${aws_eip.ip.public_ip}"
}
All the variables are declared in the terraform.tfvars
amis = {
"ubuntu-server" = "ami-0b76c3b150c6b1423"
}
Comments
Post a Comment
commented your blog