AWS Amazon EC2 Instance
This page shows how to write Terraform and CloudFormation for Amazon EC2 Instance and write them securely.
aws_instance (Terraform)
The Instance in Amazon EC2 can be configured in Terraform with the resource name aws_instance
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}
resource "aws_instance" "web1" {
resource "aws_instance" "instance1" {
ami = "fake_ami"
instance_type = "m3.medium"
root_block_device {
volume_size = 10
resource "aws_instance" "foo" {
}
resource "aws_instance" "bar" {
provider = notaws
}
resource "aws_instance" "machine1" {
ami = "ami-04b9e92b5572fa0d1"
instance_type = "t2.micro"
}
resource "aws_instance" "machine2" {
ami = "ami-04b9e92b5572fa0d1"
resource "aws_instance" "machine1" {
ami = "ami-04b9e92b5572fa0d1"
instance_type = "t2.micro"
}
resource "aws_instance" "machine2" {
ami = "ami-04b9e92b5572fa0d1"
Security Best Practices for aws_instance
There are 2 settings in aws_instance that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to avoid storing AWS access keys in user data
It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.
Ensure HTTP tokens are required for IMDS
It is better to require session tokens for all access to IMDS. IMDS (Instance Metadata Service) allows access without session tokens, which could be abused by several attacks like server-side request forgery (SSRF).
Parameters
-
ami
required - string -
arn
optional computed - string -
associate_public_ip_address
optional computed - bool -
availability_zone
optional computed - string -
cpu_core_count
optional computed - number -
cpu_threads_per_core
optional computed - number -
disable_api_termination
optional - bool -
ebs_optimized
optional - bool -
get_password_data
optional - bool -
hibernation
optional - bool -
host_id
optional computed - string -
iam_instance_profile
optional - string -
id
optional computed - string -
instance_initiated_shutdown_behavior
optional - string -
instance_state
optional computed - string -
instance_type
required - string -
ipv6_address_count
optional computed - number -
ipv6_addresses
optional computed - list of string -
key_name
optional computed - string -
monitoring
optional - bool -
outpost_arn
optional computed - string -
password_data
optional computed - string -
placement_group
optional computed - string -
primary_network_interface_id
optional computed - string -
private_dns
optional computed - string -
private_ip
optional computed - string -
public_dns
optional computed - string -
public_ip
optional computed - string -
secondary_private_ips
optional computed - set of string -
security_groups
optional computed - set of string -
source_dest_check
optional - bool -
subnet_id
optional computed - string -
tags
optional - map from string to string -
tenancy
optional computed - string -
user_data
optional - string -
user_data_base64
optional - string -
volume_tags
optional - map from string to string -
vpc_security_group_ids
optional computed - set of string -
credit_specification
list block-
cpu_credits
optional - string
-
-
ebs_block_device
set block-
delete_on_termination
optional - bool -
device_name
required - string -
encrypted
optional computed - bool -
iops
optional computed - number -
kms_key_id
optional computed - string -
snapshot_id
optional computed - string -
tags
optional - map from string to string -
throughput
optional computed - number -
volume_id
optional computed - string -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
enclave_options
list block-
enabled
optional computed - bool
-
-
ephemeral_block_device
set block-
device_name
required - string -
no_device
optional - bool -
virtual_name
optional - string
-
-
metadata_options
list block-
http_endpoint
optional computed - string -
http_put_response_hop_limit
optional computed - number -
http_tokens
optional computed - string
-
-
network_interface
set block-
delete_on_termination
optional - bool -
device_index
required - number -
network_interface_id
required - string
-
-
root_block_device
list block-
delete_on_termination
optional - bool -
device_name
optional computed - string -
encrypted
optional computed - bool -
iops
optional computed - number -
kms_key_id
optional computed - string -
tags
optional - map from string to string -
throughput
optional computed - number -
volume_id
optional computed - string -
volume_size
optional computed - number -
volume_type
optional computed - string
-
-
timeouts
single block
Explanation in Terraform Registry
Provides an EC2 instance resource. This allows instances to be created, updated, and deleted. Instances also support provisioning.
Tips: Best Practices for The Other AWS Amazon EC2 Resources
In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
aws_ebs_volume
Ensure to use a customer-managed key for EBS volume encryption
It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
AWS::EC2::Instance (CloudFormation)
The Instance in EC2 can be configured in CloudFormation with the resource name AWS::EC2::Instance
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::Instance
Properties:
ImageId: "ami-3ecc8f46"
InstanceType: "t2.micro"
KeyName: "AWS Key Pair 1"
SecurityGroups:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: !Ref AmiId
KeyName: !Ref KeyPairName
IamInstanceProfile: !Ref InstanceProfileName
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: !Ref AmiId
KeyName: !Ref KeyPairName
IamInstanceProfile: !Ref InstanceProfileName
Type: AWS::EC2::Instance
Properties:
InstanceType:
Ref: InstanceType
KeyName:
Ref: KeyName
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref 'AMIIDMaster'
InstanceType: !Ref 'InstanceTypeMaster'
KeyName: !Ref 'KeyName'
PlacementGroupName: !Ref 'PlacementGroup'
"Type" : "AWS::EC2::Instance",
"Properties" : {
"AvailabilityZone" : "us-gov-east-1a",
"DisableApiTermination" : false,
"ImageId" : "ami-05f4133d4fdbd61f3",
"InstanceType" : "t3.medium",
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t1.micro",
"ImageId": "ami-b12da5d1",
"KeyName": {
"Ref": "KeyName"
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : "ami-1b458c72",
"InstanceType" : "m1.xlarge"
}
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : "ami-1b458c72",
"InstanceType" : "m1.xlarge"
}
Parameters
-
AdditionalInfo
optional - String -
Affinity
optional - String -
AvailabilityZone
optional - String -
BlockDeviceMappings
optional - List of BlockDeviceMapping -
CpuOptions
optional - CpuOptions -
CreditSpecification
optional - CreditSpecification -
DisableApiTermination
optional - Boolean -
EbsOptimized
optional - Boolean -
ElasticGpuSpecifications
optional - List of ElasticGpuSpecification -
ElasticInferenceAccelerators
optional - List of ElasticInferenceAccelerator -
EnclaveOptions
optional - EnclaveOptions -
HibernationOptions
optional - HibernationOptions -
HostId
optional - String -
HostResourceGroupArn
optional - String -
IamInstanceProfile
optional - String -
ImageId
optional - String -
InstanceInitiatedShutdownBehavior
optional - String -
InstanceType
optional - String -
Ipv6AddressCount
optional - Integer -
Ipv6Addresses
optional - List of InstanceIpv6Address -
KernelId
optional - String -
KeyName
optional - String -
LaunchTemplate
optional - LaunchTemplateSpecification -
LicenseSpecifications
optional - List of LicenseSpecification -
Monitoring
optional - Boolean -
NetworkInterfaces
optional - List of NetworkInterface -
PlacementGroupName
optional - String -
PrivateIpAddress
optional - String -
RamdiskId
optional - String -
SecurityGroupIds
optional - List -
SecurityGroups
optional - List -
SourceDestCheck
optional - Boolean -
SsmAssociations
optional - List of SsmAssociation -
SubnetId
optional - String -
Tags
optional - List of Tag -
Tenancy
optional - String -
UserData
optional - String -
Volumes
optional - List of Volume
Explanation in CloudFormation Registry
Specifies an EC2 instance.
If an Elastic IP address is attached to your instance, AWS CloudFormation reattaches the Elastic IP address after it updates the instance. For more information about updating stacks, see AWS CloudFormation Stacks Updates.
Frequently asked questions
What is AWS Amazon EC2 Instance?
AWS Amazon EC2 Instance is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon EC2 Instance?
For Terraform, the N4gato/tfgitflow, gilyas/infracost and terraform-docs/terraform-config-inspect source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the OpenStorageNetwork/ansible-osn, nanalakshmanan/AwsApiActionDemo and nanalakshmanan/AwsApiActionDemo source code examples are useful. See the CloudFormation Example section for further details.