Skip to content

Instantly share code, notes, and snippets.

@s0enke
Last active May 24, 2022 00:14
Show Gist options
  • Save s0enke/98f3fff56adbf51a1c21864dda09862b to your computer and use it in GitHub Desktop.
Save s0enke/98f3fff56adbf51a1c21864dda09862b to your computer and use it in GitHub Desktop.
Generate AWS Service Catalog Products from CDK Constructs
// Possible since https://github.com/aws/aws-cdk/pull/17144
class VpcProduct extends servicecatalog.ProductStack {
vpc: aws_ec2.Vpc;
constructor(scope: Construct, id: string) {
super(scope, id);
this.vpc = new aws_ec2.Vpc(this, 'Vpc', {
natGateways: 0
});
}
}
interface FargateProductProps extends StackProps {
vpc: aws_ec2.IVpc;
}
class FargateProduct extends servicecatalog.ProductStack {
constructor(scope: Construct, id: string, props: FargateProductProps) {
super(scope, id);
new aws_ecs_patterns.ApplicationLoadBalancedFargateService(this, 'FargateService', {
vpc: props.vpc,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample')
},
taskSubnets: props.vpc.selectSubnets({subnetType: aws_ec2.SubnetType.PUBLIC}),
assignPublicIp: true,
});
}
}
export class CdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const portfolio = new servicecatalog.Portfolio(this, 'Portfolio', {
displayName: "My awesome portfolio",
providerName: "The awesome CCoE"
});
const vpcProductStack = new VpcProduct(this, 'VpcProduct');
const vpcProduct = new servicecatalog.CloudFormationProduct(this, 'VpcProductStack', {
productName: "VPC",
owner: "The awesome CCoE",
productVersions: [
{
productVersionName: "0.0.1",
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(vpcProductStack),
},
],
});
portfolio.addProduct(vpcProduct);
const fargateProduct = new servicecatalog.CloudFormationProduct(this, 'FargateProductStack', {
productName: "ECS Fargate with ALB",
owner: "The awesome CCoE",
productVersions: [
{
productVersionName: "0.0.3",
cloudFormationTemplate: servicecatalog.CloudFormationTemplate.fromProductStack(new FargateProduct(this, 'FargateProduct', {vpc: vpcProductStack.vpc})),
},
],
});
portfolio.addProduct(fargateProduct);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment