<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Balaji S</title>
    <description>The latest articles on DEV Community by Balaji S (@balaji_s).</description>
    <link>https://dev.to/balaji_s</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1359344%2Fc270a075-66af-4fdb-afb7-54da14561b1a.jpg</url>
      <title>DEV Community: Balaji S</title>
      <link>https://dev.to/balaji_s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/balaji_s"/>
    <language>en</language>
    <item>
      <title>Automating Security Group Updates with Dynamic Prefix Lists using Lambda and EventBridge Scheduler</title>
      <dc:creator>Balaji S</dc:creator>
      <pubDate>Fri, 29 Mar 2024 21:17:09 +0000</pubDate>
      <link>https://dev.to/balaji_s/automating-security-group-updates-withdynamic-prefix-lists-using-lambda-andeventbridge-scheduler-500l</link>
      <guid>https://dev.to/balaji_s/automating-security-group-updates-withdynamic-prefix-lists-using-lambda-andeventbridge-scheduler-500l</guid>
      <description>&lt;p&gt;In today's cloud-centric environment, security is paramount. As organizations leverage various services and APIs provided by external entities, ensuring that only authorized access is allowed becomes increasingly challenging. One such scenario involves dynamically managing the ingress rules of security groups in AWS to permit access from specific IP addresses. In this blog post, we'll explore how to automate the updating of security group rules by dynamically adding prefix lists using AWS Lambda and EventBridge Scheduler.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;strong&gt;Introduction to the Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Consider a scenario where your organization relies on services from a third-party provider like Stripe or Github, which regularly updates its IP addresses for webhook communications. To maintain a secure infrastructure, you need to ensure that your AWS security group allows traffic only from these IP addresses.&lt;/p&gt;

&lt;p&gt;Traditionally, this process involves manual intervention, requiring someone to update the security group rules each time the third-party provider changes its IP addresses. However, with the power of automation provided by AWS Lambda and EventBridge Scheduler, we can streamline this process and ensure that our security group rules are always up-to-date.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution Overview
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;AWS Lambda Function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our Lambda function will be written in Python and will perform the following steps:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fetch the latest IP addresses from the third-party provider (in this case, Stripe) by making an HTTP request to their endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new prefix list with the fetched IP addresses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update the security group to reference the new prefix list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete the old prefix list if it exists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can find the complete code for the Lambda function here&amp;nbsp; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create a Layer in the Lambda:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkwrjo3cyvw1aqvj07rrz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkwrjo3cyvw1aqvj07rrz.png" alt="Image description" width="800" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Increase the Execution Timeout of the lambda:
&lt;/h3&gt;

&lt;p&gt;Go inside the lambda Function &amp;gt;&amp;gt;&amp;gt; Configuration &amp;gt;&amp;gt;&amp;gt; Increase the Timeout&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5mvtiqq09ibjwbjrcrt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5mvtiqq09ibjwbjrcrt.png" alt="Image description" width="800" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Lambda Function Code
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;import json&lt;br&gt;
import requests&lt;br&gt;
import boto3&lt;br&gt;
from botocore.exceptions import ClientError&lt;br&gt;
ec2 = boto3.client('ec2')&lt;br&gt;
def lambda_handler(event, context):&lt;br&gt;
    security_group_id = 'YOUR SECURITY GROUP ID'&lt;br&gt;
    response = requests.get('https://stripe.com/files/ips/ips_webhooks.json')&lt;br&gt;
    ips_data = response.json()&lt;br&gt;
    webhook_ips = ips_data['WEBHOOKS']&lt;br&gt;
    cidr_blocks = [f"{ip}/32" for ip in webhook_ips]&lt;br&gt;
    # Delete previous prefix list and detach from security group if exists&lt;br&gt;
    delete_and_detach_prefix_list('StripeIPs', security_group_id)&lt;br&gt;
    # Create a new prefix list&lt;br&gt;
    prefix_list_id = create_prefix_list('StripeIPs', cidr_blocks)&lt;br&gt;
    # Update security group to reference the new prefix list&lt;br&gt;
    update_security_group(security_group_id, prefix_list_id)&lt;br&gt;
    return {&lt;br&gt;
        'statusCode': 200,&lt;br&gt;
        'body': json.dumps('The security group ingress rules have been successfully updated with the latest Webhook IP addresses of Stripe services')&lt;br&gt;
    }&lt;br&gt;
def create_prefix_list(prefix_list_name, cidr_blocks):&lt;br&gt;
    try:&lt;br&gt;
        response = ec2.create_managed_prefix_list(&lt;br&gt;
            PrefixListName=prefix_list_name,&lt;br&gt;
            AddressFamily='IPv4',&lt;br&gt;
            Entries=[{'Cidr': cidr, 'Description': 'Stripe IP'} for cidr in cidr_blocks],&lt;br&gt;
            MaxEntries=len(cidr_blocks)&lt;br&gt;
        )&lt;br&gt;
        return response['PrefixList']['PrefixListId']&lt;br&gt;
    except ClientError as e:&lt;br&gt;
        print(f"Error creating prefix list: {str(e)}")&lt;br&gt;
        raise e&lt;br&gt;
def delete_and_detach_prefix_list(prefix_list_name, security_group_id):&lt;br&gt;
    try:&lt;br&gt;
        response = ec2.describe_managed_prefix_lists(&lt;br&gt;
            Filters=[&lt;br&gt;
                {'Name': 'prefix-list-name', 'Values': [prefix_list_name]}&lt;br&gt;
            ]&lt;br&gt;
        )&lt;br&gt;
        prefix_lists = response.get('PrefixLists', [])&lt;br&gt;
        if prefix_lists:&lt;br&gt;
            old_prefix_list_id = prefix_lists[0]['PrefixListId']&lt;br&gt;
            # Detach the old prefix list from the security group&lt;br&gt;
            detach_prefix_list_from_security_group(security_group_id, old_prefix_list_id)&lt;br&gt;
            # Delete the old prefix list&lt;br&gt;
            ec2.delete_managed_prefix_list(PrefixListId=old_prefix_list_id)&lt;br&gt;
    except ClientError as e:&lt;br&gt;
        print(f"Error deleting old prefix list: {str(e)}")&lt;br&gt;
        raise e&lt;br&gt;
def detach_prefix_list_from_security_group(security_group_id, prefix_list_id):&lt;br&gt;
    try:&lt;br&gt;
        ec2.revoke_security_group_ingress(&lt;br&gt;
            GroupId=security_group_id,&lt;br&gt;
            IpPermissions=[&lt;br&gt;
                {&lt;br&gt;
                    'PrefixListIds': [{'PrefixListId': prefix_list_id}],&lt;br&gt;
                    'IpProtocol': 'tcp',&lt;br&gt;
                    'FromPort': 80,&lt;br&gt;
                    'ToPort': 80,&lt;br&gt;
                },&lt;br&gt;
                {&lt;br&gt;
                    'PrefixListIds': [{'PrefixListId': prefix_list_id}],&lt;br&gt;
                    'IpProtocol': 'tcp',&lt;br&gt;
                    'FromPort': 443,&lt;br&gt;
                    'ToPort': 443,&lt;br&gt;
                }&lt;br&gt;
            ]&lt;br&gt;
        )&lt;br&gt;
    except ClientError as e:&lt;br&gt;
        print(f"Error detaching prefix list from security group: {str(e)}")&lt;br&gt;
        raise e&lt;br&gt;
def update_security_group(security_group_id, prefix_list_id):&lt;br&gt;
    try:&lt;br&gt;
        ec2.authorize_security_group_ingress(&lt;br&gt;
            GroupId=security_group_id,&lt;br&gt;
            IpPermissions=[&lt;br&gt;
                {&lt;br&gt;
                    'PrefixListIds': [{'PrefixListId': prefix_list_id}],&lt;br&gt;
                    'IpProtocol': 'tcp',&lt;br&gt;
                    'FromPort': 80,&lt;br&gt;
                    'ToPort': 80,&lt;br&gt;
                },&lt;br&gt;
                {&lt;br&gt;
                    'PrefixListIds': [{'PrefixListId': prefix_list_id}],&lt;br&gt;
                    'IpProtocol': 'tcp',&lt;br&gt;
                    'FromPort': 443,&lt;br&gt;
                    'ToPort': 443,&lt;br&gt;
                }&lt;br&gt;
            ]&lt;br&gt;
        )&lt;br&gt;
    except ClientError as e:&lt;br&gt;
        print(f"Error updating security group: {str(e)}")&lt;br&gt;
        raise e&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember to replace placeholders like&amp;nbsp;SECURITY_GROUP_ID&amp;nbsp;with actual values from your AWS environment. With this setup, you can ensure that your AWS security group rules are always aligned with the latest IP addresses provided by your third-party services, enhancing the security of your infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;

&lt;p&gt;1.&amp;nbsp;## Importing Libraries:&lt;br&gt;
 The function starts by importing necessary libraries:&amp;nbsp;JSON&amp;nbsp;for JSON handling,&amp;nbsp;requests&amp;nbsp;for making HTTP requests,&amp;nbsp;boto3&amp;nbsp;for AWS SDK, and&amp;nbsp;ClientError&amp;nbsp;from&amp;nbsp;botocore. exceptions&amp;nbsp;for handling AWS client errors.&lt;/p&gt;

&lt;p&gt;2.&amp;nbsp;&amp;nbsp;## Initializing EC2 Client:&lt;br&gt;
 An EC2 client is initialized using&amp;nbsp;boto3.client('ec2'). This client enables interaction with EC2 services such as managing security groups and prefix lists.&lt;/p&gt;

&lt;p&gt;3.&amp;nbsp; ## Lambda Handler Function:&lt;br&gt;
 The&amp;nbsp;lambda_handler&amp;nbsp;function is the entry point for the Lambda execution. It receives two parameters,&amp;nbsp;event&amp;nbsp;and&amp;nbsp;context, although they are not used in this specific implementation.&lt;/p&gt;

&lt;p&gt;4.&amp;nbsp; ## Fetching IP Addresses:&lt;br&gt;
 The Lambda function makes an HTTP GET request to Stripe's endpoint to fetch the latest IP addresses for Webhook services. The fetched data is then parsed as JSON.&lt;/p&gt;

&lt;p&gt;5.&amp;nbsp; ## Converting IP Addresses:&lt;br&gt;
 The fetched IP addresses are converted to CIDR notation (x.x.x.x/32) and stored in&amp;nbsp;cidr_blocks.&lt;/p&gt;

&lt;p&gt;6.&amp;nbsp; ## Deleting and Detaching Previous Prefix List:&lt;br&gt;
 Any existing prefix list with the name 'StripeIPs' is deleted and detached from the specified security group to ensure a clean slate.&lt;/p&gt;

&lt;p&gt;7.&amp;nbsp; ## Creating New Prefix List:&lt;br&gt;
 A new prefix list named 'StripeIPs' is created using the fetched CIDR blocks.&lt;/p&gt;

&lt;p&gt;8.&amp;nbsp; ## Updating Security Group:&lt;br&gt;
 The security group specified by&amp;nbsp;security_group_id&amp;nbsp;is updated to reference the new prefix list created in the previous step. In this case, the security group allows inbound traffic on TCP ports 80 and 443 from the IP addresses specified in the prefix list.&lt;/p&gt;

&lt;p&gt;9.&amp;nbsp; ## Return Response:&lt;br&gt;
 Finally, the function returns a response indicating the successful update of the security group rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. EventBridge Scheduler
&lt;/h3&gt;

&lt;p&gt;We'll set up a rule in EventBridge Scheduler to trigger our Lambda function at a specified schedule. We'll configure it to run every night at midnight using the cron expression 0 0 * ? *&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20df8z7pbqkrhxaqmplr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20df8z7pbqkrhxaqmplr.png" alt="Image description" width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5wpz43uy49njmc1307e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5wpz43uy49njmc1307e.png" alt="Image description" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhevs5i9kz27w45mhosts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhevs5i9kz27w45mhosts.png" alt="Image description" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2c5hhknmaums6e3ta72.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2c5hhknmaums6e3ta72.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. AWS IAM Roles
&lt;/h3&gt;

&lt;p&gt;We need to create IAM roles with the following policies to grant our Lambda function the necessary permissions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lambda Execution Policy: Grants permissions to execute Lambda functions and interact with EC2 security group rules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EC2 Management Policy: Provides permissions for managing prefix lists, security groups, and related resources.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find the detailed IAM policies here.&lt;/p&gt;

&lt;h3&gt;
  
  
  IAM Roles
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Lambda Execution Policy
&lt;/h3&gt;

&lt;p&gt;This policy allows the Lambda function to execute and interact with EC2 security group rules.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": "logs:CreateLogGroup",&lt;br&gt;
            "Resource": "arn:aws:logs:REGION:ACCOUNT NUMBER:*"&lt;br&gt;
        },&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": [&lt;br&gt;
                "logs:CreateLogStream",&lt;br&gt;
                "logs:PutLogEvents"&lt;br&gt;
            ],&lt;br&gt;
            "Resource": [&lt;br&gt;
                "arn:aws:logs:REGION:ACCOUNT NUMBER:log-group:/aws/lambda/LAMBDA_FUNCTION_NAME:*"&lt;br&gt;
            ]&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Sid": "VisualEditor0",&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": [&lt;br&gt;
                "*",&lt;br&gt;
                "*"&lt;br&gt;
            ],&lt;br&gt;
            "Resource": [&lt;br&gt;
                "arn:aws:ec2:REGION:ACCOUNT NUMBER:security-group-rule/*",&lt;br&gt;
                "arn:aws:ec2:REGION:ACCOUNT NUMBER:security-group/SECURITY_GROUP"&lt;br&gt;
            ]&lt;br&gt;
        },&lt;br&gt;
        {&lt;br&gt;
            "Sid": "VisualEditor1",&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": "ec2:DescribeSecurityGroupRules",&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": [&lt;br&gt;
                "ec2:CreateManagedPrefixList",&lt;br&gt;
                "ec2:ModifyManagedPrefixList",&lt;br&gt;
                "ec2:DeleteManagedPrefixList",&lt;br&gt;
                "ec2:DescribeManagedPrefixLists"&lt;br&gt;
            ],&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  EC2 Management Policy
&lt;/h3&gt;

&lt;p&gt;This policy provides permissions for managing prefix lists, security groups, and related resources.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": [&lt;br&gt;
                "ec2:DescribeSecurityGroups",&lt;br&gt;
                "ec2:RevokeSecurityGroupIngress",&lt;br&gt;
                "ec2:DescribePrefixLists",&lt;br&gt;
                "ec2:CreatePrefixList",&lt;br&gt;
                "ec2:DeletePrefixList",&lt;br&gt;
                "ec2:ModifyPrefixList"&lt;br&gt;
            ],&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": "ec2:CreateManagedPrefixList",&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        },&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": "ec2:AuthorizeSecurityGroupIngress",&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "Version": "2012-10-17",&lt;br&gt;
    "Statement": [&lt;br&gt;
        {&lt;br&gt;
            "Effect": "Allow",&lt;br&gt;
            "Action": "ec2:DescribePrefixLists",&lt;br&gt;
            "Resource": "*"&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace&amp;nbsp;REGION,&amp;nbsp;ACCOUNT_ID, and&amp;nbsp;YOUR_FUNCTION_NAME&amp;nbsp;with the appropriate values for your AWS environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting It All Together
&lt;/h2&gt;

&lt;p&gt;Once we have set up our Lambda function, EventBridge Scheduler rule, and IAM roles, the automation workflow will function as follows:&lt;/p&gt;

&lt;p&gt;1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EventBridge Scheduler triggers the Lambda function at the scheduled time (every night at midnight).&lt;/p&gt;

&lt;p&gt;2.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The Lambda function fetches the latest IP addresses from the third-party provider.&lt;/p&gt;

&lt;p&gt;3.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; It creates a new prefix list and updates the security group to reference this new list.&lt;/p&gt;

&lt;p&gt;4.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If an old prefix list with the same name exists, it is deleted.&lt;/p&gt;

&lt;p&gt;5.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The security group now allows traffic only from the latest IP addresses provided by the third-party provider.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post, we have explored how to automate the updating of AWS security group rules using Lambda and EventBridge Scheduler. By leveraging these AWS services, we can ensure that our infrastructure remains secure and up-to-date without manual intervention. This approach not only saves time and effort but also reduces the risk of human error.&lt;/p&gt;

&lt;p&gt;By implementing similar automation workflows, organizations can enhance their security posture and adapt to changes in external services seamlessly. As the cloud landscape continues to evolve, embracing automation becomes essential for maintaining a robust and secure infrastructure.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>aws</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
