This project contains a Rust-based AWS Lambda function that stops EC2 instances based on specific tags. The function is designed to look for EC2 instances with the tag AutoTerminate
set to true
and stop them.
Cargo.toml
: Contains the metadata and dependencies for the Rust project.Cargo.lock
: Locks the dependencies to specific versions.src/main.rs
: The main Rust source code file containing the Lambda function logic..gitignore
: Specifies which files and directories Git should ignore.
- Rust programming language installed.
- Docker installed (for building the Lambda function for AWS).
- AWS CLI configured with the necessary permissions.
TAG_NAME
: The name of the tag used to identify EC2 instances for stopping.TAG_VALUE
: The value of the tag used to identify EC2 instances for stopping.
const TAG_NAME: &str = "AutoTerminate";
const TAG_VALUE: &str = "true";
Use Docker to build the Lambda function for the x86_64-unknown-linux-musl
target:
docker build -t ec2-terminator .
container_id=$(docker create ec2-terminator)
docker cp ${container_id}:/app/bootstrap ./bootstrap
docker rm ${container_id}
zip lambda.zip bootstrap
Create the Lambda function using the AWS CLI:
aws lambda create-function --function-name ec2Terminator \
--handler bootstrap \
--runtime provided.al2 \
--role arn:aws:iam::741238249954:role/service-role/avbo-test-role-h7x0j96b \
--zip-file fileb://lambda.zip --region eu-central-1
If the Lambda function already exists, you can update it:
aws lambda update-function-code --function-name ec2Terminator --zip-file fileb://lambda.zip --region eu-central-1
-
Initialization:
- The Lambda function initializes and sets up logging.
-
Event Handling:
- The function is triggered by an AWS event (such as a CloudWatch event or API Gateway request).
- It retrieves the AWS configuration and creates an EC2 client.
-
Instance Identification:
- The function describes EC2 instances with the tag
AutoTerminate
set totrue
.
- The function describes EC2 instances with the tag
-
Stopping Instances:
- It stops the identified EC2 instances.
- Logs the details of stopped instances for audit purposes.
-
Response:
- The function returns a response indicating the instances that were stopped.
This setup ensures that any EC2 instance tagged with AutoTerminate: true
will be automatically stopped when the Lambda function is triggered, helping to manage costs and resources efficiently.