Automatically update security groups for Amazon CloudFront IP ranges using AWS Lambda
February 8, 2022: You can now use the new managed prefix list for CloudFront in your Security Group instead of this custom Lambda solution.
Amazon CloudFront is a content delivery network that can help you increase the performance of your web applications and significantly lower the latency of delivering content to your customers. For CloudFront to access an origin (the source of the content behind CloudFront), the origin has to be publicly available and reachable. Anyone with the origin domain name or IP address could request content directly and bypass CloudFront. In this blog post, I describe an automated solution that uses security groups to permit only CloudFront to access the origin.
Amazon Simple Storage Service (Amazon S3) origins provide a feature called Origin Access Identity, which blocks public access to selected buckets, making them accessible only through CloudFront. When you use CloudFront to secure your web applications, it’s important to ensure that only CloudFront can access your origin (such as Amazon Elastic Cloud Compute (Amazon EC2) or Application Load Balancer (ALB)) and any direct access to origin is restricted. This blog post shows you how to create an AWS Lambda function to automatically update Amazon Virtual Private Cloud (Amazon VPC) security groups with CloudFront service IP ranges to permit only CloudFront to access the origin.
AWS publishes the IP ranges in JSON format for CloudFront and other AWS services. If your origin is an Elastic Load Balancer or an Amazon EC2 instance, you can use VPC security groups to allow only CloudFront IP ranges to access your applications. The IP ranges in the list are separated by service and Region, and you must specify only the IP ranges that correspond to CloudFront.
The IP ranges that AWS publishes change frequently and without an automated solution, you would need to retrieve this document frequently to understand the current IP ranges for CloudFront. Frequent polling is inefficient because there is no notice of when the IP ranges change, and if these IP ranges aren’t modified immediately, your client might see 504 errors when they access CloudFront. Additionally, there are numerous IP ranges for each service, performing the change manually isn’t an efficient way of updating these ranges. This means you need infrastructure to support the task. However, in that case you end up with another host to manage—complete with the typical patching, deployment, and monitoring. As you can see, a small task could quickly become more complicated than the problem you intended to solve.
An Amazon Simple Notification Service (Amazon SNS) message is sent to a topic whenever the AWS IP ranges change. Enabling you to build an event-driven, serverless solution that updates the IP ranges for your security groups, as needed by using a Lambda function that is triggered in response to the SNS notification.
Here are the steps we are going to take to implement the solution:
- Create your resources
- Create an IAM policy and execution role for the Lambda function
- Create your Lambda function
- Test your Lambda function
- Configure your Lambda function’s trigger
Create your resources
The first thing you need to do is create a Lambda function execution role and policy. Lambda function uses execution role to access or create AWS resources. This Lambda function is triggered by an SNS notification whenever there’s a change in the IP ranges document. Based on the number of IP ranges present for CloudFront and also the number of ports (for example, 80,443) that you want to whitelist on the origin, this Lambda function creates the required security groups. These security groups will allow only traffic from CloudFront to your ELB load balancers or EC2 instances.
Create an IAM policy and execution role for the Lambda function
When you create a Lambda function, it’s important to understand and properly define the security context for the Lambda function. Using AWS Identity and Access Management (IAM), you can create the Lambda execution role that determines the AWS service calls that the function is authorized to complete. (Learn more about the Lambda permissions model.)
To create the IAM policy for your role
- Log in to the IAM console with the user account that you will use to manage the Lambda function. This account must have administrator permissions.
- In the navigation pane, choose Policies.
- In the content pane, choose Create policy.
- Choose the JSON tab and copy the text from the following JSON policy document. Paste this text into the JSON text box.
- When you’re finished, choose Review policy.
- On the Review page, enter a name for the policy name (e.g. LambdaExecRolePolicy-UpdateSecurityGroupsForCloudFront). Review the policy Summary to see the permissions granted by your policy, and then choose Create policy to save your work.
To understand what this policy allows, let’s look closely at both statements in the policy. The first statement allows the Lambda function to create and write to CloudWatch Logs, which is vital for debugging and monitoring our function. The second statement allows the function to get information about existing security groups, get existing VPC information, create security groups, and authorize and revoke ingress permissions. It’s an important best practice that your IAM policies be as granular as possible, to support the principal of least privilege.
Now that you’ve created your policy, you can create the Lambda execution role that will use the policy.
To create the Lambda execution role
- In the navigation pane of the IAM console, choose Roles, and then choose Create role.
- For Select type of trusted entity, choose AWS service.
- Choose the service that you want to allow to assume this role. In this case, choose Lambda.
- Choose Next: Permissions.
- Search for the policy name that you created earlier and select the check box next to the policy.
- Choose Next: Tags.
- (Optional) Add metadata to the role by attaching tags as key-value pairs. For more information about using tags in IAM, see Tagging IAM Users and Roles.
- Choose Next: Review.
- For Role name (e.g. LambdaExecRole-UpdateSecurityGroupsForCloudFront), enter a name for your role.
- (Optional) For Role description, enter a description for the new role.
- Review the role, and then choose Create role.
Create your Lambda function
Now, create your Lambda function and configure the role that you created earlier as the execution role for this function.
To create the Lambda function
- Go to the Lambda console in N. Virginia region and choose Create function. On the next page, choose Author from scratch. (I’ll be providing the code for your Lambda function, but for other functions, the Use a blueprint option can be a great way to get started.)
- Give your Lambda function a name (e.g UpdateSecurityGroupsForCloudFront) and description, and select Python 3.8 from the Runtime menu.
- Choose or create an execution role: Select the execution role you created earlier by selecting the option Use an Existing Role.
- After confirming that your settings are correct, choose Create function.
- Paste the Lambda function code from here.
- Select Save.
Additionally, in the Basic Settings of the Lambda function, increase the timeout to 10 seconds.
To set the timeout value in the Lambda console
- In the Lambda console, choose the function you just created.
- Under Basic settings, choose Edit.
- For Timeout, select 10s.
- Choose Save.
By default, the Lambda function has these settings:
- The Lambda function is configured to create security groups in the default VPC.
- CloudFront IP ranges are updated as inbound rules on port 80.
- The created security groups are tagged with the name prefix AUTOUPDATE.
- Debug logging is turned off.
- The service for which IP ranges are extracted is set to CloudFront.
- The SDK client in the Lambda function set to us-east-1(N. Virginia).
If you want to customize these settings, set the following environment variables for the Lambda function. For more details, see Using AWS Lambda environment variables.
|To create security groups in a specific VPC||Key: VPC_ID
|To create security groups rules for a different port or multiple ports
The solution in this example supports a total of two ports. One can be used for HTTP and another for HTTPS.
|To customize the prefix name tag of your security groups||Key: PREFIX_NAME
|To enable debug logging to CloudWatch||Key: DEBUG
|To extract IP ranges for a different service other than CloudFront||Key: SERVICE
|To configure the Region for the SDK client used in the Lambda function
If the CloudFront origin is present in a different Region than N. Virginia, the security groups must be created in that region.
To set environment variables in the Lambda console
- In the Lambda console, choose the function you created.
- Under Environment variables, choose Edit.
- Choose Add environment variable.
- Enter a key and value.
- Choose Save.
Test your Lambda function
Now that you’ve created your function, it’s time to test it and initialize your security group.
To create your test event for the Lambda function
- In the Lambda console, on the Functions page, choose your function. In the drop-down menu next to Actions, choose Configure test events.
- Enter an Event Name (e.g. TriggerSNS)
- Replace the following as your sample event, which will represent an SNS notification and then select Create.
- After you’ve added the test event, select Save and then select Test. Your Lambda function is then invoked, and you should see log output at the bottom of the console in Execution Result section, similar to the following.
- Edit the sample event again, and this time change the md5 value in the sample event to be the first MD5 hash provided in the log output. In this example, you would update the md5 value in the sample event configured earlier with the hash value seen in the error ‘2e967e943cf98ae998efeec05d4f351c’. Lambda code successfully executes only when the original hash of the IP ranges document and the hash received from the event trigger match. After you modify the hash value from the error message received earlier, the test event matches the hash of the IP ranges document.
- Select Save and test. This invokes your Lambda function.
After the function is invoked the second time with updated md5 has Lambda function should execute without any errors. You should be able to see the new security groups created and the IP ranges of CloudFront updated in the rules in the EC2 console, as shown in Figure 1.
In the initial successful run of this function, it created the total number of security groups required to update all the IP ranges of CloudFront for the ports mentioned. The function creates security groups based on the maximum number of rules that can be added to individual security groups. The new security groups can be identified from the EC2 console by the name AUTOUPDATE_random if you used the default configuration, or a custom name if you provided a PREFIX_NAME.
Configure your Lambda function’s trigger
After you’ve validated that your function is executing properly, it’s time to connect it to the SNS topic for IP changes. To do this, use the AWS Command Line Interface (CLI). Enter the following command, making sure to replace Lambda ARN with the Amazon Resource Name (ARN) of your Lambda function. You can find this ARN at the top right when viewing the configuration of your Lambda function.
You should receive the ARN of your Lambda function’s SNS subscription.
Now add a permission that allows the Lambda function to be invoked by the SNS topic. The following command also adds the Lambda trigger.
When AWS changes any of the IP ranges in the document, an SNS notification is sent and your Lambda function will be triggered. This Lambda function verifies the modified ranges in the document and efficiently updates the IP ranges on the existing security groups. Additionally, the function dynamically scales and creates additional security groups if the number of IP ranges for CloudFront is increased in future. Any newly created security groups are automatically attached to the network interface where the previous security groups are attached in order to avoid service interruption.
As you followed this blog post, you created a Lambda function to create a security groups and update the security group’s rules dynamically whenever AWS publishes new internal service IP ranges. This solution has several advantages:
- The solution isn’t designed as a periodic poll, so it only runs when it needs to.
- It’s automatic, so you don’t need to update security groups manually which lowers the operational cost.
- It’s simple, because you have no extra infrastructure to maintain as the solution is completely serverless.
- It’s cost effective, because the Lambda function runs only when triggered by the AmazonIpSpaceChanged SNS topic and only runs for a few seconds, this solution costs only pennies to operate.
If you have feedback about this post, submit comments in the Comments section below. If you have questions about this post, start a new thread on the Amazon CloudFront forum. If you have any other use cases for using Lambda functions to dynamically update security groups, or even other networking configurations such as VPC route tables or ACLs, we’d love to hear about them!
Want more AWS Security how-to content, news, and feature announcements? Follow us on Twitter.