Create an EC2 Instance with Terraform: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=Internal= * Amazon EC2 Operations * Terraform AWS Operations") |
|||
(21 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
* https://www.terraform.io/docs/providers/aws/r/instance.html | |||
=Internal= | =Internal= | ||
* [[Amazon_EC2_Operations#Create_an_EC2_Instance_with_Terraform|Amazon EC2 Operations]] | * [[Amazon_EC2_Operations#Create_an_EC2_Instance_with_Terraform|Amazon EC2 Operations]] | ||
* [[Terraform AWS Operations#Create_an_EC2_Instance|Terraform AWS Operations]] | * [[Terraform AWS Operations#Create_an_EC2_Instance|Terraform AWS Operations]] | ||
=Overview= | |||
EC2 instances can be created, updated and deleted. Instances also support [[Terraform_Concepts#Provisioning|provisioning]]. | |||
=Playground= | |||
{{External|https://github.com/ovidiuf/playground/tree/master/hashicorp/terraform/simplest-ec2-instance}} | |||
=Terraform Registry ec2-instance Module= | |||
{{External|https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/2.8.0}} | |||
=Configuration= | |||
<syntaxhighlight lang='json'> | |||
provider "aws" { | |||
profile = "default" | |||
region = "us-west-2" | |||
} | |||
resource "aws_instance" "terraform-experiment-01" { | |||
ami = "ami-000000000000000" | |||
instance_type = "t2.small" | |||
subnet_id = "subnet-0000000000000000" | |||
key_name = "my-keypair-01" | |||
security_groups = ["sg-0000000000000000"] | |||
associate_public_ip_address = false | |||
iam_instance_profile = aws_iam_instance_profile.some-profile.name | |||
tags = { | |||
Name = "terraform-experiment-01" | |||
} | |||
}</syntaxhighlight> | |||
The instance profile can be created by the same module or a dependency module. If it is created by a dependency sub-module, it can be referred as: | |||
<syntaxhighlight lang='json'> | |||
resource "aws_instance" "terraform-experiment-01" { | |||
... | |||
iam_instance_profile = ${module.aws-iam.some-profile} | |||
}</syntaxhighlight> | |||
=Configuration Details= | |||
==Public IP== | |||
If nothing is specified, the instance will get a public IP. To disable allocation of a public address specify: | |||
<syntaxhighlight lang='json'> | |||
resource "aws_instance" ... { | |||
... | |||
associate_public_ip_address = false | |||
... | |||
} | |||
</syntaxhighlight> | |||
Note that even if <code>associate_public_ip_address</code> is explicitly set to "true", if the associated subnet has a <code>map_public_ip_on_launch</code> = true, a public IP will be associated anyway. | |||
==Multiple Instances== | |||
<syntaxhighlight lang='json'> | |||
variable "instances" { | |||
default = "2" | |||
} | |||
resource "aws_instance" "my-instance" { | |||
count = "${var.instances}" | |||
... | |||
tags = { | |||
Name = "my-instance-${count.index + 1}" | |||
} | |||
} | |||
</syntaxhighlight> | |||
==Block Device Mapping== |
Latest revision as of 22:10, 10 December 2019
External
Internal
Overview
EC2 instances can be created, updated and deleted. Instances also support provisioning.
Playground
Terraform Registry ec2-instance Module
Configuration
provider "aws" {
profile = "default"
region = "us-west-2"
}
resource "aws_instance" "terraform-experiment-01" {
ami = "ami-000000000000000"
instance_type = "t2.small"
subnet_id = "subnet-0000000000000000"
key_name = "my-keypair-01"
security_groups = ["sg-0000000000000000"]
associate_public_ip_address = false
iam_instance_profile = aws_iam_instance_profile.some-profile.name
tags = {
Name = "terraform-experiment-01"
}
}
The instance profile can be created by the same module or a dependency module. If it is created by a dependency sub-module, it can be referred as:
resource "aws_instance" "terraform-experiment-01" {
...
iam_instance_profile = ${module.aws-iam.some-profile}
}
Configuration Details
Public IP
If nothing is specified, the instance will get a public IP. To disable allocation of a public address specify:
resource "aws_instance" ... {
...
associate_public_ip_address = false
...
}
Note that even if associate_public_ip_address
is explicitly set to "true", if the associated subnet has a map_public_ip_on_launch
= true, a public IP will be associated anyway.
Multiple Instances
variable "instances" {
default = "2"
}
resource "aws_instance" "my-instance" {
count = "${var.instances}"
...
tags = {
Name = "my-instance-${count.index + 1}"
}
}