Skip to content

Study Project: AWS Lambda function that automatically associates a specific Elastic IP address to EC2 instances when they are successfully launched by an Auto Scaling group.

License

Notifications You must be signed in to change notification settings

finkj/lambda.ec2.eip.launch.handler

Repository files navigation

lambda.ec2.eip.launch.handler

AWS Lambda function that automatically associates a specific Elastic IP address to EC2 instances when they are successfully launched by an Auto Scaling group.

Overview

This Lambda function responds to EC2 instance launch events from AWS Auto Scaling groups and associates a pre-configured Elastic IP address to instances that match specific tag filters. This is particularly useful for applications running on EC2 Spot instances where the IP address needs to remain consistent across instance replacements.

Note: This is primarily a learning project. For production environments, consider using AWS Load Balancers for more robust solutions.

Use Case

When running applications on EC2 Spot instances, AWS may terminate, stop, or hibernate instances when capacity is needed. This function ensures that the Elastic IP address is automatically reassociated to newly launched instances, maintaining a consistent public IP for your application.

Architecture

  • Trigger: EventBridge rule monitoring EC2 Instance Launch Successful events from AWS Auto Scaling
  • Handler: Lambda function filters instances by tags and associates the configured EIP
  • Runtime: Node.js 18.x
  • AWS Services: EC2, EventBridge, Lambda

Project Structure

├── src/
│   ├── aws-clients/
│   │   └── ec2client.mjs       # EC2 client wrapper functions
│   ├── events/
│   │   └── instancelaunch.json # Sample event for testing
│   ├── handlers/
│   │   ├── app.mjs             # Main Lambda handler
│   │   └── config.json         # EIP configuration
│   └── schema/
│       └── aws.autoscaling.instance.launch.successful.json
├── template.yaml               # AWS SAM template
├── package.json               # Node.js dependencies
└── env.json                   # Environment variables for local testing

Configuration

The function uses the following environment variables (configured in template.yaml):

  • Region: AWS region (default: eu-central-1)
  • TagsFilter: Tag value to identify target instances (default: example-tag)

The Elastic IP allocation ID is configured in src/handlers/config.json.

Deployment

Deploy using AWS SAM CLI:

sam build
sam deploy --guided

During deployment, you'll be prompted to configure:

  • Stack name
  • AWS region
  • CloudFormation capabilities (CAPABILITY_IAM required)

Required IAM Permissions

The Lambda function requires the following permissions:

  • AWSLambdaBasicExecutionRole - CloudWatch Logs access
  • AmazonEC2ReadOnlyAccess - Read EC2 instance details and tags
  • Custom policy to associate Elastic IP addresses

Local Testing

Test locally using AWS SAM CLI:

sam local invoke EC2LaunchFunction --event src/events/instancelaunch.json

Dependencies

  • @aws-sdk/client-ec2: ^3.312.0
  • jest: ^29.2.1 (dev dependency)

Cleanup

To delete the deployed application:

aws cloudformation delete-stack --stack-name <your-stack-name>

About

Study Project: AWS Lambda function that automatically associates a specific Elastic IP address to EC2 instances when they are successfully launched by an Auto Scaling group.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published