AWS Amazon EC2 Association

This page shows how to write Terraform and CloudFormation for Amazon EC2 Association and write them securely.

aws_eip_association (Terraform)

The Association in Amazon EC2 can be configured in Terraform with the resource name aws_eip_association. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

ec2.tf#L40
resource "aws_eip_association" "eip_aterm" {
  instance_id   = aws_instance.ansible-terminal.id
  allocation_id = aws_eip.eip_aterm.id
}
resource "aws_eip_association" "eip_atarg1" {
  instance_id   = aws_instance.ansible-target1.id
eip.tf#L1
resource "aws_eip_association" "rocketchat-eip" {
  instance_id = aws_instance.rocket-chat-instance.id
  allocation_id = "eipalloc-099aa273162640dc3"
}

resource "aws_eip_association" "ssh-bastion-eip" {
main.tf#L46
resource "aws_eip_association" "eip_assoc_1" {
  instance_id   = module.ec2_labs.aws_instance_e_ssh[0]
  allocation_id = aws_eip.exemplo_1.id
}

resource "aws_eip_association" "eip_assoc_2" {
eip.tf#L1
resource "aws_eip_association" "rocketchat-eip" {
  instance_id = aws_instance.rocket-chat-instance.id
  allocation_id = "eipalloc-099aa273162640dc3"
}

resource "aws_eip_association" "ssh-bastion-eip" {

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 AWS EIP Association as a top level resource, to associate and disassociate Elastic IPs from AWS Instances and Network Interfaces.

NOTE: Do not use this resource to associate an EIP to aws_lb or aws_nat_gateway resources. Instead use the allocation_id available in those resources to allow AWS to manage the association, otherwise you will see AuthFailure errors. NOTE: aws_eip_association is useful in scenarios where EIPs are either pre-existing or distributed to customers or users and therefore cannot be changed.

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::EIPAssociation (CloudFormation)

The EIPAssociation in EC2 can be configured in CloudFormation with the resource name AWS::EC2::EIPAssociation. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

VPC_EC2_Instance_With_ENI.yml#L7
    Type: AWS::EC2::EIPAssociation
    Properties:
      AllocationId: !GetAtt ControlPortAddress.AllocationId
      NetworkInterfaceId: !Ref controlXface
  WebPortAddress:
    Type: AWS::EC2::EIP
cloudformation.yml#L62
        Type: AWS::EC2::EIPAssociation
        Properties:
            InstanceId: !Ref 'BankEC2Instance'
            EIP: '18.184.49.117'
    PostgresIPAssoc:
        Type: AWS::EC2::EIPAssociation
wait_tpl.yml#L38
    type: "AWS::EC2::EIPAssociation"
    properties:
      EIP:
        Ref: service1_floating
      InstanceId:
        Ref: service1
SingleENIwithMultipleEIPs.yml#L37
    Type: 'AWS::EC2::EIPAssociation'
    DependsOn:
      - ENI
      - EIP1
    Properties:
      AllocationId: !GetAtt
SingleENIwithMultipleEIPs.yml#L37
    Type: 'AWS::EC2::EIPAssociation'
    DependsOn:
      - ENI
      - EIP1
    Properties:
      AllocationId: !GetAtt
SingleENIwithMultipleEIPs.json#L26
   "Type": "AWS::EC2::EIPAssociation",
   "DependsOn" : ["ENI","EIP1"],
   "Properties": {
      "AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
      "NetworkInterfaceId": {"Ref":"ENI"},
      "PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
SingleENIwithMultipleEIPs.json#L26
   "Type": "AWS::EC2::EIPAssociation",
   "DependsOn" : ["ENI","EIP1"],
   "Properties": {
      "AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
      "NetworkInterfaceId": {"Ref":"ENI"},
      "PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
SingleENIwithMultipleEIPs.json#L26
   "Type": "AWS::EC2::EIPAssociation",
   "DependsOn" : ["ENI","EIP1"],
   "Properties": {
      "AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
      "NetworkInterfaceId": {"Ref":"ENI"},
      "PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
SingleENIwithMultipleEIPs.json#L26
   "Type": "AWS::EC2::EIPAssociation",
   "DependsOn" : ["ENI","EIP1"],
   "Properties": {
      "AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
      "NetworkInterfaceId": {"Ref":"ENI"},
      "PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
aws_cf_http2study_stack.json#L155
            "Type" : "AWS::EC2::EIPAssociation",
            "Properties" : {
                "AllocationId" : { "Fn::GetAtt" : [ "hiControlAddress", "AllocationId" ]},
                "NetworkInterfaceId" : { "Ref" : "hiControlSubnetInterface" }
            }
        },

Parameters

Explanation in CloudFormation Registry

Associates an Elastic IP address with an instance or a network interface. Before you can use an Elastic IP address, you must allocate it to your account.

An Elastic IP address is for use in either the EC2-Classic platform or in a VPC. For more information, see Elastic IP Addresses in the Amazon EC2 User Guide.

[EC2-Classic, VPC in an EC2-VPC-only account] If the Elastic IP address is already associated with a different instance, it is disassociated from that instance and associated with the specified instance. If you associate an Elastic IP address with an instance that has an existing Elastic IP address, the existing address is disassociated from the instance, but remains allocated to your account.

[VPC in an EC2-Classic account] If you don't specify a private IP address, the Elastic IP address is associated with the primary IP address. If the Elastic IP address is already associated with a different instance or a network interface, you get an error unless you allow reassociation. You cannot associate an Elastic IP address with an instance or network interface that has an existing Elastic IP address.

Frequently asked questions

What is AWS Amazon EC2 Association?

AWS Amazon EC2 Association 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 Association?

For Terraform, the yirez/ansible-tests, davidgalstyan53/awsrocket and trevisangabriel/devops source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the j-crotty/CloudFormation, nikitsenka/bank-flux and fzhaw/blog source code examples are useful. See the CloudFormation Example section for further details.