In this rapidly growing world of cloud computing, it's crucial to maintain a consistent and dependable connection to your resources. Amazon Web Services (AWS) provides a dynamic solution for Elastic IPs and static public IP addresses for your EC2 instances. Earlier, associating an Elastic IP needed a separate allocation and association procedure. However, now, AWS has launched a new feature where you can simply associate Elastic IPs directly during the EC2 instance launch. With this, you can easily configure an Elastic IP to be instantly associated with your EC2 instance at creation time.Understanding Elastic IPs: A Public IP Address on DemandBy default, the EC2 instance is allocated a private IP address within your Virtual Private Cloud (VPC) or the conventional EC2 network. While this enables internal communication within your AWS infrastructure, it blocks external access from the internet. Let's understand where Elastic IPs step in.Elastic IP is like enhanced public IP addresses. You can easily assign static IP addresses to your EC2 instance, providing a reliable and consistent entry gate for inbound connections. This certainly proves invaluable in scenarios like:Web servers: They enable you to grant public access to your website or application hosted on an EC2 instance. Remote desktop access: With Elastic IPs, you get secure remote connections to your EC2 instance for administrative purposes. Game servers: They offer a reliable and consistent public IP address for players to link to your game server.Elastic IPs offer a plethora of benefits over conventional public IP addresses allocated by your ISP:Flexibility: You can associate and disassociate Elastic IPs from EC2 instances effortlessly, providing full and dynamic control over public accessibility. Durability: Elastic IPs are different from public IP addresses that can change with reboots. However, Elastic IPs are much more persistent and stay available even after instance termination. Cost-effectiveness: You have to pay for the time an Elastic IP is allocated and not for the complete billing cycle.Prerequisites for Association: Gearing Up for SuccessBefore heading towards the association journey, make sure that you hold the following with you:An AWS Account: First, Sign up for a free tier account if you're new to AWS https://aws.amazon.com/free/. An EC2 Instance: Then, take another step by launching an EC2 instance in the region of your choice. However, ensure it's running well in a public subnet with internet gateway access for external communication. Security Groups: To permit inbound traffic on the ports you use regularly, like port 80 for web traffic or port 22 for SSH access, you need to configure security groups.Pro Tip: When launching your EC2 instance, you can pick the option to "Assign a public IP address" during configuration. This way, the allocation of Elastic IP to your instance automatically gets done. Yet, for other granular control and cost optimization, the manual association process is considered.Step-by-Step Guide For Association ProcessNow, let's see the association process by navigating via the AWS management console:Access the EC2 Console: Firstly, log in to the AWS Management Console and then navigate to the EC2 service. Navigate to Elastic IPs: Locate the "Elastic IPs" section in the navigation pane. Allocate a New Address: Now click on the option "Allocate Elastic IP address". Choose Allocation Method: You need to choose either "EC2-Classic" or "VPC" based on where your EC2 instance is actually residing. In the case of most modern deployments, VPCs are required. Allocate the Address: Click on the button "Allocate" to confirm the allocation further. Then, you'll see a newly formed Elastic IP with a public IP address assigned to it.Voila, you've successfully allocated an Elastic IP. Now, you need to associate it with your EC2 instance. Let's dive in.Locate the Elastic IP: Check the list of Elastic IPs and then recognize the one you have just allocated. Associate the Address: Then, right-click on the Elastic IP and choose the option "Associate Address". Choose the Target Instance: You'll see a pop-up window where you need to choose the EC2 instance you desire to associate the Elastic IP with from the "Instance ID" dropdown menu. Specify the Private IP: You can pick the particular private IP address within your instance to associate the Elastic IP with, which is optional. It will associate with the primary private IP by default. Associate and Verify: Now click on the button "Associate" to finalize the association. After this, you'll receive a confirmation message, and the associated instance ID will be reflected further to the Elastic IP in the list.Double-check: Go back to the EC2 instances in the console region. Then, choose your instance and find the "Public IPv4 DNS" field. Now, you will see the public IP address of your associated Elastic IP, which verifies your successful configuration.Note: You can also go through the below videoAssociation Process by CLIStep 1: Allocate an Elastic IP Addressaws ec2 allocate-address --domain vpcStep 2: Associate the Elastic IP Address with the EC2 Instanceaws ec2 associate-address --instance-id i-1234567890abcdef0 --allocation-id eipalloc-12345678Step 3: Checking the Associationaws ec2 describe-instances --instance-id i-0abcdef1234567890Step-by-Step Guide For Association Process by TerraformStep 1: Create an EC2 instance.Step 2: provider "aws" { region = "us-west-2" # Specify your desired AWS region } # Step 1: Create a security group (optional) resource "aws_security_group" "example" { name = "allow_ssh" description = "Allow SSH inbound traffic" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } # Step 2: Launch an EC2 instance (optional) resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" # Specify the AMI ID for your region instance_type = "t2.micro" security_groups = [aws_security_group.example.name] tags = { Name = "Terraform-EC2" } } # Step 3: Allocate an Elastic IP (optional) resource "aws_eip" "example" { vpc = true } # Step 4: Associate Elastic IP with EC2 instance resource "aws_eip_association" "example" { instance_id = aws_instance.example.id allocation_id = aws_eip.example.id } Step 3: terraform initStep 4: terraform validateStep 5: terraform applyHow to set up Elastic IP failover?Step 1: Create Elastic IPStep 2: Assign to Primary InstanceStep 3: Write Failover Script#!/bin/bash PRIMARY_INSTANCE_ID=i-xxxxxxxx SECONDARY_INSTANCE_ID=i-yyyyyyyy EIP=eipalloc-zzzzzzzz # Check if the primary instance is healthy INSTANCE_STATE=$(aws ec2 describe-instances --instance-id $PRIMARY_INSTANCE_ID --query "Reservations[0].Instances[0].State.Name" --output text) if [ "$INSTANCE_STATE" != "running" ]; then # Disassociate EIP from primary instance aws ec2 disassociate-address --association-id $(aws ec2 describe-addresses --allocation-ids $EIP --query "Addresses[0].AssociationId" --output text) # Associate EIP with secondary instance aws ec2 associate-address --instance-id $SECONDARY_INSTANCE_ID --allocation-id $EIP fiStep 4: Automate it with CronJob*/5 * * * * /path/to/failover-script.shElastic IP CostingElastic IP ScenarioCostAssociated with a running instanceNo chargeNot associated with a running instance or associated with a stopped instance$0.005 per hourRemapping (more than 100 times per month)$0.10 per remapAdvanced Techniques and Best Practices for Elastic IPDisassociation and Release: Manage the disassociation properly and release by navigating further to the Elastic IPs section in the EC2 console. You can disassociate an Elastic IP to block the connection between the Elastic IP and the EC2 instance and release it if there's no further requirement. Automating Associations: Facilitate the procedure of associating Elastic IPs for huge-scale deployments with the help of automation tools like AWS CLI and SDKs for different programming languages. Optimizing Billing: Check properly Elastic IP usage to find out and release any unused IPs. Then, try using Schedules Instances or Spot Instances for cost savings, and make sure you pay only for the IPs active currently. Security Considerations: Apply top-notch security practices like configuring security groups to permit inbound traffic only on important ports, using IAM roles for access control, and checking EC2 instances for any suspicious activity to improve security.ConclusionElastic IPs certify you to develop consistent and dependable connections to your EC2 instances in the AWS cloud. When you understand the allocation, association, and management processes, you can use the flexibility and cost-effectiveness of Elastic IPs. Just keep in mind that you should implement automation for efficiency, optimize billing to control costs, and prioritize security to protect your resources. With all these practices in place, you'll be well on your journey to achieving consistency in connection and maximizing the value of your Elastic IPs in your AWS deployments.Read Morehttps://devopsden.io/article/what-is-nginxFollow us onhttps://www.linkedin.com/company/devopsden/