I want to setup a infrastructure for a client product and I need to share resources between different ECS Services like VPC, Security Group, RDS so I can avoid recreating EC2 instance and assign a global accelerator to that ec2 instance for static ip
There's no particular barrier to sharing resources across stacks in the CDK. Just access the objects like any other object.
Create your shared resources in one stack, then simply access them from your other stacks. The CDK will automatically create necessary cloudformation exports and imports.
For example, suppose you create a stack infraStack
which contains your vpc, security groups, etc as properties. You can simply pass your infraStack
object to the constructor for your other stacks and access the resources in the other stack then use the objects like accessing any object like infraStack.vpc
.
Alternatively, you can explicitly create the exports/imports. For example, you can use CfnOutput to output the id for a VPC. Then you can import this in another stack using importValue
from Fn and use that with ec2.Vpc.fromLookup
-- or if you don't care about hard-coding the VPC id, just call ec2.Vpc.fromLookup
and hard-code the VPC ID.