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
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
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" {
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" {
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" {
Parameters
-
allocation_id
optional computed - string -
allow_reassociation
optional - bool -
id
optional computed - string -
instance_id
optional computed - string -
network_interface_id
optional computed - string -
private_ip_address
optional computed - string -
public_ip
optional computed - string
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
oraws_nat_gateway
resources. Instead use theallocation_id
available in those resources to allow AWS to manage the association, otherwise you will seeAuthFailure
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.
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::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
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt ControlPortAddress.AllocationId
NetworkInterfaceId: !Ref controlXface
WebPortAddress:
Type: AWS::EC2::EIP
Type: AWS::EC2::EIPAssociation
Properties:
InstanceId: !Ref 'BankEC2Instance'
EIP: '18.184.49.117'
PostgresIPAssoc:
Type: AWS::EC2::EIPAssociation
type: "AWS::EC2::EIPAssociation"
properties:
EIP:
Ref: service1_floating
InstanceId:
Ref: service1
Type: 'AWS::EC2::EIPAssociation'
DependsOn:
- ENI
- EIP1
Properties:
AllocationId: !GetAtt
Type: 'AWS::EC2::EIPAssociation'
DependsOn:
- ENI
- EIP1
Properties:
AllocationId: !GetAtt
"Type": "AWS::EC2::EIPAssociation",
"DependsOn" : ["ENI","EIP1"],
"Properties": {
"AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
"NetworkInterfaceId": {"Ref":"ENI"},
"PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
"Type": "AWS::EC2::EIPAssociation",
"DependsOn" : ["ENI","EIP1"],
"Properties": {
"AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
"NetworkInterfaceId": {"Ref":"ENI"},
"PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
"Type": "AWS::EC2::EIPAssociation",
"DependsOn" : ["ENI","EIP1"],
"Properties": {
"AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
"NetworkInterfaceId": {"Ref":"ENI"},
"PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
"Type": "AWS::EC2::EIPAssociation",
"DependsOn" : ["ENI","EIP1"],
"Properties": {
"AllocationId": { "Fn::GetAtt" : [ "EIP1", "AllocationId" ]},
"NetworkInterfaceId": {"Ref":"ENI"},
"PrivateIpAddress": {"Fn::Select" : [ "0", {"Fn::GetAtt" : [ "ENI" , "SecondaryPrivateIpAddresses"]} ]}
"Type" : "AWS::EC2::EIPAssociation",
"Properties" : {
"AllocationId" : { "Fn::GetAtt" : [ "hiControlAddress", "AllocationId" ]},
"NetworkInterfaceId" : { "Ref" : "hiControlSubnetInterface" }
}
},
Parameters
-
AllocationId
optional - String -
EIP
optional - String -
InstanceId
optional - String -
NetworkInterfaceId
optional - String -
PrivateIpAddress
optional - String
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.