- Newest
- Most votes
- Most comments
When you're working with security groups that were created by one IaC tool (Pulumi) and then referencing them in another tool (CDK), you can encounter conflicts in how the resources are managed.
The issue you're experiencing is related to how CDK handles security group rules when using add_ingress_rule with an existing security group. When you reference the Pulumi-created security group and set remote_rule=False, CDK is trying to modify the security group rules directly, which can conflict with Pulumi's state management.
To avoid CDK modifying or deleting rules that were created with Pulumi, you should import the existing security group as an immutable reference rather than trying to modify it. Here's how you can approach this:
- Import the existing security group using
fromSecurityGroupId:
alb_security_group = ec2.SecurityGroup.from_security_group_id( self, "ImportedALBSecurityGroup", service_config.security_groups_inbound, mutable=False # This is important - marks it as immutable )
- Then use this imported security group reference in your ingress rule:
security_group.add_ingress_rule( ec2.Peer.security_group_id(alb_security_group.security_group_id), ec2.Port.tcp(service_config.host_port), "Ingress from ALB" )
Alternatively, if you need to maintain bidirectional rules between security groups, you might need to use CloudFormation exports/imports to reference the security group IDs. This approach involves:
- Exporting the security group ID from Pulumi
- Importing it in CDK using
Fn.importValue - Using
SecurityGroup.fromSecurityGroupIdto reference it
This way, CDK will only manage the rules for the security groups it creates, and won't attempt to modify the Pulumi-created security groups.
When working with mixed IaC tools, it's generally best to keep clear boundaries between what each tool manages to avoid conflicts.
Sources
class SecurityGroup (construct) · AWS CDK
CDK - Importing and modifying security group from a previous stack | AWS re:Post
Relevant content
asked 2 years ago
asked 3 years ago
asked 4 years ago
asked 4 years ago
