AWS Amazon EC2 EIP

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

aws_eip (Terraform)

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

Example Usage from GitHub

eip.tf#L21
resource "aws_eip" "eipalloc-dev-nat-eu-central-1c" { // 0ce2e574181a9bb6b
    network_interface = aws_network_interface.eni-dev-nat-eu-central-1c.id // "eni-0e906faf95f71552b"
    vpc               = true
}

resource "aws_eip" "eipalloc-dev-nat-eu-central-1a" { // 0d599a316dbe98a24
ec2.tf#L16
resource "aws_eip" "kafka1" {
  instance = aws_instance.kafka1.id
  vpc      = true
}

resource "aws_instance" "kafka1" {
main.tf#L16
resource "aws_eip" "prod-ip1" {}
resource "aws_eip" "prod-ip2" {}

resource "aws_eip" "stag-ip1" {}
resource "aws_eip" "stag-ip2" {}
2-eip.tf#L4
resource "aws_eip" "tokyo_eip" {
  provider = aws.tokyo
  vpc      = "true"

  tags = {
    Name  = "salawu-live-demo"
main.tf#L17
resource "aws_eip" "prod-ip1" {}
resource "aws_eip" "prod-ip2" {}

resource "aws_eip" "stag-ip1" {}
resource "aws_eip" "stag-ip2" {}
resource "aws_eip" "stag-ip3" {}

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 Elastic IP resource.

Note: EIP may require IGW to exist prior to association. Use depends_on to set an explicit dependency on the IGW. Note: Do not use network_interface to associate the 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.

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

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

Example Usage from GitHub

nat-gateway.yml#L86
    Type: 'AWS::EC2::EIP'
    Properties:
      Tags:
        - Key: Name
          Value: !Sub '${StackName}-EIPA-${Env}'
      Domain: vpc
nat_gw.yml#L41
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc

  EIP2:
    Type: AWS::EC2::EIP
network_load_balancer.yml#L26
    Type: 'AWS::EC2::EIP'
    Properties:
      Domain: vpc

  SecondEIP:
    Type: 'AWS::EC2::EIP'
nlb.yml#L23
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc

  EipAz2:
    Type: AWS::EC2::EIP
gateways.serverless.yml#L20
    Type: AWS::EC2::EIP
    Properties:
      Tags:
        - Key: Name
          Value: ${env:APP_NAME}-eip-ngw-az1
      Domain: Vpc
NetworkLoadBalancerWithEIPs.json#L28
      "Type": "AWS::EC2::EIP",
      "Properties": {
        "Domain": "VPC"
      }
    },
    "EIP2": {
NetworkLoadBalancerWithEIPs.json#L28
      "Type": "AWS::EC2::EIP",
      "Properties": {
        "Domain": "VPC"
      }
    },
    "EIP2": {
NetworkLoadBalancerWithEIPs.json#L28
      "Type": "AWS::EC2::EIP",
      "Properties": {
        "Domain": "VPC"
      }
    },
    "EIP2": {
NetworkLoadBalancerWithEIPs.json#L28
      "Type": "AWS::EC2::EIP",
      "Properties": {
        "Domain": "VPC"
      }
    },
    "EIP2": {
elastic-ip-pool-template.json#L14
          "Type": "AWS::EC2::EIP",
          "Properties": {
            "Domain": "vpc"
          }
        },
        "EIP2": {

Parameters

Explanation in CloudFormation Registry

Specifies an Elastic IP (EIP) address and can, optionally, associate it with an Amazon EC2 instance.

You can allocate an Elastic IP address from an address pool owned by AWS or from an address pool created from a public IPv4 address range that you have brought to AWS for use with your AWS resources using bring your own IP addresses (BYOIP). For more information, see Bring Your Own IP Addresses (BYOIP) in the Amazon EC2 User Guide.

[EC2-VPC] If you release an Elastic IP address, you might be able to recover it. You cannot recover an Elastic IP address that you released after it is allocated to another AWS account. You cannot recover an Elastic IP address for EC2-Classic. To attempt to recover an Elastic IP address that you released, specify it in this operation.

An Elastic IP address is for use either in the EC2-Classic platform or in a VPC. By default, you can allocate 5 Elastic IP addresses for EC2-Classic per Region and 5 Elastic IP addresses for EC2-VPC per Region.

For more information, see Elastic IP Addresses in the Amazon EC2 User Guide.

Frequently asked questions

What is AWS Amazon EC2 EIP?

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

For Terraform, the mdigbazova/terraforming-4ha-live-all-resources, asaushkin/kafka-ansible and Dtenizbek/terraform source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the codedoeseverything/devops-infra, thbishop/kv and stelligent/cfn-model source code examples are useful. See the CloudFormation Example section for further details.