AWS Amazon EC2 Template
This page shows how to write Terraform and CloudFormation for Amazon EC2 Template and write them securely.
aws_launch_template (Terraform)
The Template in Amazon EC2 can be configured in Terraform with the resource name aws_launch_template. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_launch_template" "noncompliantawstemplate1" { # Noncompliant {{Make sure that using public IP address is safe here.}}
# ^^^^^^^^^^^^^^^^^^^^^
}
resource "aws_launch_template" "noncompliantawstemplate2" {
# ^^^^^^^^^^^^^^^^^^^^^> {{Related template}}
resource "aws_launch_template" "nf_lt_standard" {
name = "nextflow-launchtemplate-standard"
tags = var.default_tags
# ccdl-nextflow-base-v2.0 image
image_id = "ami-01c08d4de548477df"
block_device_mappings {
resource "aws_launch_template" "lt_basic" {
image_id = "fake_ami"
instance_type = "t2.medium"
block_device_mappings {
device_name = "xvdf"
resource "aws_launch_template" "k3s_server" {
name_prefix = "k3s_server_tpl"
image_id = var.AMIS[var.AWS_REGION]
instance_type = "t3.large"
user_data = data.template_cloudinit_config.k3s_server.rendered
resource "aws_launch_template" "bastion" {
name = "bastion-launch-template"
image_id = var.ami-bastion
instance_type = var.instance_type
Parameters
-
arnoptional computed - string -
default_versionoptional computed - number -
descriptionoptional - string -
disable_api_terminationoptional - bool -
ebs_optimizedoptional - string -
idoptional computed - string -
image_idoptional - string -
instance_initiated_shutdown_behavioroptional - string -
instance_typeoptional - string -
kernel_idoptional - string -
key_nameoptional - string -
latest_versionoptional computed - number -
nameoptional computed - string -
name_prefixoptional - string -
ram_disk_idoptional - string -
security_group_namesoptional - set of string -
tagsoptional - map from string to string -
update_default_versionoptional - bool -
user_dataoptional - string -
vpc_security_group_idsoptional - set of string -
block_device_mappingslist block-
device_nameoptional - string -
no_deviceoptional - string -
virtual_nameoptional - string -
ebslist block-
delete_on_terminationoptional - string -
encryptedoptional - string -
iopsoptional computed - number -
kms_key_idoptional - string -
snapshot_idoptional - string -
throughputoptional computed - number -
volume_sizeoptional computed - number -
volume_typeoptional computed - string
-
-
-
capacity_reservation_specificationlist block-
capacity_reservation_preferenceoptional - string -
capacity_reservation_targetlist block-
capacity_reservation_idoptional - string
-
-
-
cpu_optionslist block-
core_countoptional - number -
threads_per_coreoptional - number
-
-
credit_specificationlist block-
cpu_creditsoptional - string
-
-
elastic_gpu_specificationslist block-
typerequired - string
-
-
elastic_inference_acceleratorlist block-
typerequired - string
-
-
enclave_optionslist block-
enabledoptional - bool
-
-
hibernation_optionslist block-
configuredrequired - bool
-
-
iam_instance_profilelist block -
instance_market_optionslist block-
market_typeoptional - string -
spot_optionslist block-
block_duration_minutesoptional - number -
instance_interruption_behavioroptional - string -
max_priceoptional - string -
spot_instance_typeoptional - string -
valid_untiloptional computed - string
-
-
-
license_specificationset block-
license_configuration_arnrequired - string
-
-
metadata_optionslist block-
http_endpointoptional computed - string -
http_put_response_hop_limitoptional computed - number -
http_tokensoptional computed - string
-
-
monitoringlist block-
enabledoptional - bool
-
-
network_interfaceslist block-
associate_carrier_ip_addressoptional - string -
associate_public_ip_addressoptional - string -
delete_on_terminationoptional - string -
descriptionoptional - string -
device_indexoptional - number -
ipv4_address_countoptional - number -
ipv4_addressesoptional - set of string -
ipv6_address_countoptional - number -
ipv6_addressesoptional - set of string -
network_interface_idoptional - string -
private_ip_addressoptional - string -
security_groupsoptional - set of string -
subnet_idoptional - string
-
-
placementlist block-
affinityoptional - string -
availability_zoneoptional - string -
group_nameoptional - string -
host_idoptional - string -
partition_numberoptional - number -
spread_domainoptional - string -
tenancyoptional - string
-
-
tag_specificationslist block-
resource_typeoptional - string -
tagsoptional - map from string to string
-
Explanation in Terraform Registry
Provides an EC2 launch template resource. Can be used to create instances or auto scaling groups.
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_instance
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.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
AWS::EC2::LaunchTemplate (CloudFormation)
The LaunchTemplate in EC2 can be configured in CloudFormation with the resource name AWS::EC2::LaunchTemplate. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MetadataOptionsNone
LaunchTemplateData:
DisableApiTermination: true
ImageId: ami-04d5cc9b88example
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MetadataOptionsNone
LaunchTemplateData:
DisableApiTermination: true
ImageId: ami-04d5cc9b88example
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: IMDSv1Disabled
LaunchTemplateData:
DisableApiTermination: true
ImageId: ami-04d5cc9b88example
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MetadataOptionsNone
LaunchTemplateData:
DisableApiTermination: true
ImageId: ami-04d5cc9b88example
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: IMDSv1Disabled
LaunchTemplateData:
DisableApiTermination: true
ImageId: ami-04d5cc9b88example
"Type" : "AWS::EC2::LaunchTemplate",
"Properties" : {
"LaunchTemplateData" : {
"ImageId" : { "Fn::FindInMap" : [ "AmiMap", "ServerType01", "id" ] },
"InstanceType" : { "Ref" : "InstanceTypeParam" },
"KeyName" : { "Fn::FindInMap" : [ "KeyPair", "Key01", "key" ] },
"Type": "AWS::EC2::LaunchTemplate"
},
"NonCompliantEC2LaunchTemplate1": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": "ami-04169656fea786776"
}
}
"Type": "AWS::EC2::LaunchTemplate"
},
"NonCompliantEC2LaunchTemplate1": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": "ami-04169656fea786776"
}
}
Parameters
-
LaunchTemplateNameoptional - String -
LaunchTemplateDataoptional - LaunchTemplateData -
TagSpecificationsoptional - List of LaunchTemplateTagSpecification
Explanation in CloudFormation Registry
Specifies a launch template for an Amazon EC2 instance. A launch template contains the parameters to launch an instance. For more information, see Launch an instance from a launch template in the Amazon EC2 User Guide.
Frequently asked questions
What is AWS Amazon EC2 Template?
AWS Amazon EC2 Template 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 Template?
For Terraform, the SonarSource/sonar-iac, AlexsLemonade/alsf-scpca and gilyas/infracost source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the sprathod369/iac-example, melscoop-test/check and melscoop-test/check source code examples are useful. See the CloudFormation Example section for further details.