This AWS lambda function will update the DNS records for a given subdomain (defined in the Subdomain
tag on an EC2 server) to point to the public IP address of an EC2 instance.
- Create a AWS lambda function
- Set the runtime to
Node.js 20.x
- Set the env variables
CF_ZONE_ID
andCLOUDFLARE_API_TOKEN
- Create an EventBridge rule that triggers the lambda function on EC2 instance state change (see below)
- Run
pnpm install
on this repo to install the dependencies - run
pnpm build
to build the lambda function - Run
pnpm deploy
to deploy the lambda function to AWS (assuming your lambda function is namedDNS_Updater
. Make sure you are logged into theaws
CLI.)
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "CloudFormation template for EventBridge RuleDNSUpdater_Instance_Start",
"Resources": {
"Rule0a21bc73": {
"Type": "AWS::Events::Rule",
"Properties": {
"Name": "DNSUpdater_Instance_Start",
"EventPattern": "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"],\"detail\":{\"state\":[\"running\"]}}",
"State": "ENABLED",
"EventBusName": "default",
"Targets": [{
"Id": "Id0a53661e-f906-481a-a0e5-ddb594a44468",
"Arn": {
"Fn::Sub": "arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:DNS_Updater"
}
}]
}
}
},
"Parameters": {}
}
{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"state": ["running"]
}
}