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
-
amirequired - string -
arnoptional computed - string -
associate_public_ip_addressoptional computed - bool -
availability_zoneoptional computed - string -
cpu_core_countoptional computed - number -
cpu_threads_per_coreoptional computed - number -
disable_api_terminationoptional - bool -
ebs_optimizedoptional - bool -
get_password_dataoptional - bool -
hibernationoptional - bool -
host_idoptional computed - string -
iam_instance_profileoptional - string -
idoptional computed - string -
instance_initiated_shutdown_behavioroptional - string -
instance_stateoptional computed - string -
instance_typerequired - string -
ipv6_address_countoptional computed - number -
ipv6_addressesoptional computed - list of string -
key_nameoptional computed - string -
monitoringoptional - bool -
outpost_arnoptional computed - string -
password_dataoptional computed - string -
placement_groupoptional computed - string -
primary_network_interface_idoptional computed - string -
private_dnsoptional computed - string -
private_ipoptional computed - string -
public_dnsoptional computed - string -
public_ipoptional computed - string -
secondary_private_ipsoptional computed - set of string -
security_groupsoptional computed - set of string -
source_dest_checkoptional - bool -
subnet_idoptional computed - string -
tagsoptional - map from string to string -
tenancyoptional computed - string -
user_dataoptional - string -
user_data_base64optional - string -
volume_tagsoptional - map from string to string -
vpc_security_group_idsoptional computed - set of string -
credit_specificationlist block-
cpu_creditsoptional - string
-
-
ebs_block_deviceset block-
delete_on_terminationoptional - bool -
device_namerequired - string -
encryptedoptional computed - bool -
iopsoptional computed - number -
kms_key_idoptional computed - string -
snapshot_idoptional computed - string -
tagsoptional - map from string to string -
throughputoptional computed - number -
volume_idoptional computed - string -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
enclave_optionslist block-
enabledoptional computed - bool
-
-
ephemeral_block_deviceset block-
device_namerequired - string -
no_deviceoptional - bool -
virtual_nameoptional - string
-
-
metadata_optionslist block-
http_endpointoptional computed - string -
http_put_response_hop_limitoptional computed - number -
http_tokensoptional computed - string
-
-
network_interfaceset block-
delete_on_terminationoptional - bool -
device_indexrequired - number -
network_interface_idrequired - string
-
-
root_block_devicelist block-
delete_on_terminationoptional - bool -
device_nameoptional computed - string -
encryptedoptional computed - bool -
iopsoptional computed - number -
kms_key_idoptional computed - string -
tagsoptional - map from string to string -
throughputoptional computed - number -
volume_idoptional computed - string -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
timeoutssingle 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
-
AdditionalInfooptional - String -
Affinityoptional - String -
AvailabilityZoneoptional - String -
BlockDeviceMappingsoptional - List of BlockDeviceMapping -
CpuOptionsoptional - CpuOptions -
CreditSpecificationoptional - CreditSpecification -
DisableApiTerminationoptional - Boolean -
EbsOptimizedoptional - Boolean -
ElasticGpuSpecificationsoptional - List of ElasticGpuSpecification -
ElasticInferenceAcceleratorsoptional - List of ElasticInferenceAccelerator -
EnclaveOptionsoptional - EnclaveOptions -
HibernationOptionsoptional - HibernationOptions -
HostIdoptional - String -
HostResourceGroupArnoptional - String -
IamInstanceProfileoptional - String -
ImageIdoptional - String -
InstanceInitiatedShutdownBehavioroptional - String -
InstanceTypeoptional - String -
Ipv6AddressCountoptional - Integer -
Ipv6Addressesoptional - List of InstanceIpv6Address -
KernelIdoptional - String -
KeyNameoptional - String -
LaunchTemplateoptional - LaunchTemplateSpecification -
LicenseSpecificationsoptional - List of LicenseSpecification -
Monitoringoptional - Boolean -
NetworkInterfacesoptional - List of NetworkInterface -
PlacementGroupNameoptional - String -
PrivateIpAddressoptional - String -
RamdiskIdoptional - String -
SecurityGroupIdsoptional - List -
SecurityGroupsoptional - List -
SourceDestCheckoptional - Boolean -
SsmAssociationsoptional - List of SsmAssociation -
SubnetIdoptional - String -
Tagsoptional - List of Tag -
Tenancyoptional - String -
UserDataoptional - String -
Volumesoptional - 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.