<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Uday Pradhan on Medium]]></title>
        <description><![CDATA[Stories by Uday Pradhan on Medium]]></description>
        <link>https://medium.com/@udaypradhan?source=rss-b216db2e1099------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*l-ncAQgGZtVWPsAI.</url>
            <title>Stories by Uday Pradhan on Medium</title>
            <link>https://medium.com/@udaypradhan?source=rss-b216db2e1099------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:15:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@udaypradhan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dynamically Creating Security Group Ingress Rules using AWS CloudFormation Templates]]></title>
            <link>https://awstip.com/dynamically-creating-security-group-ingress-rules-in-aws-cloudformation-bb3f48b049ba?source=rss-b216db2e1099------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb3f48b049ba</guid>
            <category><![CDATA[aws-security-group]]></category>
            <category><![CDATA[cloudformation-template]]></category>
            <category><![CDATA[aws-cloudformation]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Uday Pradhan]]></dc:creator>
            <pubDate>Mon, 18 Nov 2024 05:20:42 GMT</pubDate>
            <atom:updated>2024-11-19T15:49:12.362Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this blog post, I’ll walk you through a solution for creating a security group in AWS that allows SSH access (port 22) from multiple CIDR blocks specified by the user. This approach leverages the AWS::LanguageExtensions transform, which makes it easier to iterate over input parameters and create resources dynamically. This is particularly useful in scenarios where you need to handle a variable number of inputs, such as a list of IP addresses.</p><h3>Problem Statement</h3><p>AWS CloudFormation is a powerful tool for managing infrastructure as code, but it has limitations when it comes to iterating over lists to create multiple resources. For example, if you want to create a security group with ingress rules for multiple IP CIDR blocks, you typically need to manually define each rule, or use complex workarounds like macros or custom resources.</p><h3>Solution Overview</h3><p>With the introduction of AWS::LanguageExtensions, we can simplify this process by using the Fn::ForEach intrinsic function to iterate over a list of CIDR blocks. This allows us to dynamically create a security group ingress rule for each CIDR block without writing repetitive code.</p><h3>CloudFormation Template</h3><p>Here’s the CloudFormation template that demonstrates this solution:</p><pre>AWSTemplateFormatVersion: &#39;2010-09-09&#39;<br>Transform: &#39;AWS::LanguageExtensions&#39;<br>Description: &gt;<br>  AWS CloudFormation template to create a Security Group allowing ingress to port 22.<br><br>Parameters:<br>  VpcId:<br>    Description: The ID of the VPC where the security group will be created.<br>    Type: AWS::EC2::VPC::Id<br><br>  CidrBlocks:<br>    Description: Comma-delimited list of CIDR blocks allowed to access port 22.<br>    Type: CommaDelimitedList<br><br>Resources:<br>  MySecurityGroup:<br>    Type: AWS::EC2::SecurityGroup<br>    Properties:<br>      GroupDescription: Allow SSH access<br>      VpcId: !Ref VpcId<br><br>  # Create an ingress rule for each CIDR block<br>  &#39;Fn::ForEach::IngressRules&#39;:<br>    - Cidr<br>    - !Ref CidrBlocks<br>    - &#39;IngressRules&amp;{Cidr}&#39;:<br>        Type: AWS::EC2::SecurityGroupIngress<br>        Properties:<br>          GroupId: !Ref MySecurityGroup<br>          IpProtocol: tcp<br>          FromPort: 22<br>          ToPort: 22<br>          CidrIp: !Ref Cidr<br><br>Outputs:<br>  SecurityGroupId:<br>    Description: The ID of the created security group.<br>    Value: !Ref MySecurityGroup</pre><h3>How It Works</h3><ul><li>Parameters: The template accepts two parameters: VpcId for the VPC where the security group will be created, and CidrBlocks, a comma-delimited list of CIDR blocks (for example 10.0.0.1/32,65.23.147.1/32).</li><li>Resources:</li><li>MySecurityGroup: A security group is created with a description for SSH access.</li><li>Fn::ForEach::IngressRules: This section uses the Fn::ForEach function to iterate over the list of CIDR blocks. For each block, a separate AWS::EC2::SecurityGroupIngress resource is created, allowing SSH access from that CIDR.</li></ul><h3>Benefits</h3><ul><li>Simplicity: This approach reduces the need for repetitive code and manual updates when the list of CIDR blocks changes.</li><li>Scalability: Easily handle a dynamic number of CIDR blocks without modifying the template structure.</li><li>Maintainability: Simplifies updates and maintenance by centralizing the logic for creating ingress rules.</li></ul><h3>Conclusion</h3><p>The use of AWS::LanguageExtensions in CloudFormation opens up new possibilities for dynamic resource creation. By leveraging this feature, we can create more flexible and scalable infrastructure templates. This solution showcases how to efficiently manage security group ingress rules for multiple CIDR blocks, enhancing both the simplicity and maintainability of your AWS infrastructure as code.</p><p>Feel free to try this template in your AWS environment and adapt it to your needs. If you have any questions or feedback, please leave a comment below!</p><h3>Additional Tips</h3><ul><li>Testing: Before deploying in production, test the template in a development environment to ensure it behaves as expected.</li><li>Security: Always review the CIDR blocks you allow access to, ensuring they align with your security policies.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb3f48b049ba" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/dynamically-creating-security-group-ingress-rules-in-aws-cloudformation-bb3f48b049ba">Dynamically Creating Security Group Ingress Rules using AWS CloudFormation Templates</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>