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

test.tf#L29
resource "aws_launch_template" "noncompliantawstemplate1" { # Noncompliant {{Make sure that using public IP address is safe here.}}
#        ^^^^^^^^^^^^^^^^^^^^^
}

resource "aws_launch_template" "noncompliantawstemplate2" {
#        ^^^^^^^^^^^^^^^^^^^^^> {{Related template}}
nextflow-launch-templates.tf#L1
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 {
autoscaling_group_test.tf#L152
resource "aws_launch_template" "lt_basic" {
  image_id      = "fake_ami"
  instance_type = "t2.medium"

  block_device_mappings {
    device_name = "xvdf"
launchtemplate.tf#L1
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

main.tf#L3
resource "aws_launch_template" "bastion" {
  name = "bastion-launch-template"

  image_id = var.ami-bastion

  instance_type = var.instance_type

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

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.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

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).

risk-label

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.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

FAILED.yml#L4
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: MetadataOptionsNone
      LaunchTemplateData:
        DisableApiTermination: true
        ImageId: ami-04d5cc9b88example
FAILED.yml#L4
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: MetadataOptionsNone
      LaunchTemplateData:
        DisableApiTermination: true
        ImageId: ami-04d5cc9b88example
PASSED.yml#L4
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: IMDSv1Disabled
      LaunchTemplateData:
        DisableApiTermination: true
        ImageId: ami-04d5cc9b88example
FAILED.yml#L4
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: MetadataOptionsNone
      LaunchTemplateData:
        DisableApiTermination: true
        ImageId: ami-04d5cc9b88example
PASSED.yml#L4
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateName: IMDSv1Disabled
      LaunchTemplateData:
        DisableApiTermination: true
        ImageId: ami-04d5cc9b88example
05-docker01-instance-sg-e-templ-with-eth0-with-mappings-e-parameters.json#L73
        "Type" : "AWS::EC2::LaunchTemplate",
        "Properties" : {
            "LaunchTemplateData" : {
                "ImageId"      : { "Fn::FindInMap" : [ "AmiMap", "ServerType01", "id" ] },
                "InstanceType" : { "Ref" : "InstanceTypeParam" },
                "KeyName"      : { "Fn::FindInMap" : [ "KeyPair", "Key01", "key" ] },
test.json#L55
      "Type": "AWS::EC2::LaunchTemplate"
    },
    "NonCompliantEC2LaunchTemplate1": {
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
ec2_untagged_tagged.json#L39
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
          "ImageId": "ami-04169656fea786776"
        }
      }
S6329.json#L39
      "Type": "AWS::EC2::LaunchTemplate"
    },
    "NonCompliantEC2LaunchTemplate1": {
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
ec2_untagged.json#L27
      "Type": "AWS::EC2::LaunchTemplate",
      "Properties": {
        "LaunchTemplateData": {
          "ImageId": "ami-04169656fea786776"
        }
      }

Parameters

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 bridgecrewio/checkov, melscoop-test/check and melscoop-test/check source code examples are useful. See the CloudFormation Example section for further details.