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

aws_instance.tf#L1
resource "aws_instance" "web" {
  ami           = "ami-a1b2c3d4"
  instance_type = "t2.micro"
}

resource "aws_instance" "web1" {
instance_test.tf#L12
resource "aws_instance" "instance1" {
  ami           = "fake_ami"
  instance_type = "m3.medium"

  root_block_device {
    volume_size = 10
alias.tf#L1
resource "aws_instance" "foo" {
}

resource "aws_instance" "bar" {
  provider = notaws
}
main.tf#L16
resource "aws_instance" "machine1" {
 ami = "ami-04b9e92b5572fa0d1"
 instance_type = "t2.micro"
}
resource "aws_instance" "machine2" {
 ami = "ami-04b9e92b5572fa0d1"
main.tf#L16
resource "aws_instance" "machine1" {
 ami = "ami-04b9e92b5572fa0d1"
 instance_type = "t2.micro"
}
resource "aws_instance" "machine2" {
 ami = "ami-04b9e92b5572fa0d1"

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

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.

risk-label

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

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

Review your AWS Amazon EC2 settings

You can check if the aws_instance setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

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.

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_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::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

template.yml#L5
    Type: AWS::EC2::Instance
    Properties:
      ImageId: "ami-3ecc8f46"
      InstanceType: "t2.micro"
      KeyName: "AWS Key Pair 1"
      SecurityGroups:
WindowsInstances.yml#L44
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      ImageId: !Ref AmiId
      KeyName: !Ref KeyPairName
      IamInstanceProfile: !Ref InstanceProfileName
LinuxInstances.yml#L44
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      ImageId: !Ref AmiId
      KeyName: !Ref KeyPairName
      IamInstanceProfile: !Ref InstanceProfileName
lgdop-infra.yml#L40
    Type: AWS::EC2::Instance
    Properties:
      InstanceType:
        Ref: InstanceType
      KeyName:
        Ref: KeyName
HPC%20Foundation%20deployment.yml#L154
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref 'AMIIDMaster'
      InstanceType: !Ref 'InstanceTypeMaster'
      KeyName: !Ref 'KeyName'
      PlacementGroupName: !Ref 'PlacementGroup'
FullRDSBuild_rev1.json#L6
          "Type" : "AWS::EC2::Instance",
          "Properties" : {
          "AvailabilityZone" : "us-gov-east-1a",
          "DisableApiTermination" : false,
          "ImageId" : "ami-05f4133d4fdbd61f3",
          "InstanceType" : "t3.medium",
Violation-VPC-MaxSubnetPerVPC.json#L25
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "InstanceType": "t1.micro",
                "ImageId": "ami-b12da5d1",
                "KeyName": {
                    "Ref": "KeyName"
5node_cloud_formation.json#L21
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "KeyName" : { "Ref" : "KeyName" },
        "ImageId" : "ami-1b458c72",
        "InstanceType" : "m1.xlarge"
      }
5node_cloud_formation.json#L21
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "KeyName" : { "Ref" : "KeyName" },
        "ImageId" : "ami-1b458c72",
        "InstanceType" : "m1.xlarge"
      }
528884874493_Config_us-east-1_ConfigHistory_AWS__AutoScaling__AutoScalingGroup_20181011T012430Z_20181011T012430Z_1.json#L1

Parameters

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.