Home > database >  Deploy the app from github to fargate using AWS pipelines and CDK
Deploy the app from github to fargate using AWS pipelines and CDK

Time:03-20

I'm trying to set up a CI/CD from github to the AWS Fargate app using AWS CDK. I've tried using the "CDK Pipeline": https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html

And I've also tried the other one (Code Pipeline?) as well: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline-readme.html

Somehow, both of these did not have the feature I need.

I've added a step with a build action: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline_actions.CodeBuildAction.html

It worked fine and generated an artifact. Here's the code:

const buildArtifact = new Artifact();

pipeline.addStage({
  stageName: "Build",
  actions: [
    new CodeBuildAction({
      project: new PipelineProject(this, "Build", {
        projectName: "Build",
        buildSpec: BuildSpec.fromSourceFilename("./docker-compose.yml"),
      }),
      input: sources,
      outputs: [buildArtifact],
    }),
  ],
});

But then I proceeded to deploy build results to fargate, like: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.FargateTaskDefinition.html

const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
  memoryLimitMiB: 512,
  cpu: 256,
});
const container = fargateTaskDefinition.addContainer("WebContainer", {
  image: ecs.ContainerImage.from...??
});

And, it turned out, there is absolutely no way to create this container image from an artifact: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ContainerImage.html

Sadly, it was all for nothing.

How do I make a pipeline that actually works and deploys C# project to the fargate service? It's not like I'm trying to achieve something complicated- just to deploy my C# app. So, I must be doing something wrong.

CodePudding user response:

You've basically got all the pieces here. Since that addContainer function's image prop requires the type ContainerImage, we can see several options for defining that image (AssetImage and EcrImage seem to be the most relevant.)

If you want to go the ECR image route, you can just modify your build step you added to push the built container to ECR, and then use EcrImage to supply the task definition (this article has a useful buildspec.yml to work off of).

Or, you can use AssetImage, point it to the correct location in your repo, and have CDK handle the building. Note that in this use case, AssetImage is not taking in a pre-built artifact, but it is instead generating the artifact itself. This obviously requires your Dockerfile and C# code to be in the same repo as your CDK code.

  • Related