Constantin De La Roche
Constantin De La Roche

Reputation: 3320

How create a security group that allow all inbound traffic using the aws cdk?

I have recently discovered the AWS CDK and I want to create an EC2 instance with a security group that allow all inbound traffic (that's for a lab).

Using the web interface, I can do it like this: enter image description here

Now, I want to use the AWS CDK (in python) to create a security group like that. Is that possible ? If so, can I have a snippet please ?

Thanks!

Upvotes: 2

Views: 3505

Answers (1)

Constantin De La Roche
Constantin De La Roche

Reputation: 3320

I finally found a solution.

# VPC
vpc = ec2.Vpc(self, "VPC",
              nat_gateways=0,
              subnet_configuration=[ec2.SubnetConfiguration(
                  name="public", subnet_type=ec2.SubnetType.PUBLIC)]
              )
# security group allowing all traffic (in and out)
security_group = ec2.SecurityGroup(self, "SG", vpc=vpc)
security_group.add_ingress_rule(
    peer=ec2.Peer.any_ipv4(),
    connection=ec2.Port.all_traffic()
)

Warning, you should know what your doing when allowing all traffic.

Upvotes: 4

Related Questions