skip to Main Content

I want to upload a local file in the repository to s3 after it has been processed by a custom docker image with AWS CDK. I don’t want to make the docker image public (Its not a big restriction tho). Also, I don’t want to build the image for each s3 deployment

Since I don’t want to build the docker image for each bucket deployment, I have created a DockerImageAsset, and tried to give image uri as BucketDeployment‘s bundle property. Code is below:

    const image = new DockerImageAsset(this, "cv-builder-image", {
      directory: join(__dirname, "../"),
    });

    new BucketDeployment(this, "bucket-deployment", {
      destinationBucket: bucket,
      sources: [
        Source.asset(join(__dirname, "../"), {
          bundling: {
            image: DockerImage.fromRegistry(image.imageUri),
            command: [
              "bash",
              "-c",
              'echo "heloo" >> /asset-input/cv.html && cp /asset-input/cv.html /asset-output/cv.html',
            ],
          },
        }),
      ],
    });

DockerImageAsset is deployed fine. But it throw this during BucketDeployment‘s deployment

docker: invalid reference format: repository name must be lowercase

I can see the image being deployed to AWS.

Any help is appreciated. Have a nice dayy

2

Answers


  1. As far as I understand – to simplify – you have a Docker image which you use to launch a utility container that just takes a file and outputs an artifact (another file).

    Then you want to upload the artifact to S3 using the BucketDeployment construct.

    This is a common problem when dealing with compiling apps like Java to .jar artifacts or frontend applications (React, Angular) to static output (HTML, CSS, JS) files.

    The way I’ve approached this in the past is: Split the artifact generation as a separate step in your pipeline and THEN trigger the "cdk deploy" as a subsequent step.

    You would have less headache and you control all parts of the process, including having access to the low level Docker commands like docker build ... and docker run ..., and in effect, leverage local layer caching in the best possible way. If you rely on CDK to do the bundling for you – there’s a bit of magic behind the scenes that’s not always obvious. I’m not saying it’s impossible, it’s just more "work".

    Login or Signup to reply.
  2. This can be done without utilizing DockerImageAsset. In this case the Dockerfile is stored at ../src/app1.

    new BucketDeployment(this, "DeployApp1", {
      sources: [Source.asset('.', { // the '.' doesn't matter here
        bundling: {
          image: DockerImage.fromBuild(join(__dirname, '../src/app1')),
        }
      })],
      destinationBucket: s3App1,
      distribution,
      distributionPaths: ["/app1/*"]
    });
    

    Here is the sample docker image

    FROM node:lts
    
    WORKDIR /app
    COPY "." "."
    RUN npm install
    RUN npx ng build --configuration production --output-path /asset-stage
    
    # This has to be executed after the container is running
    # See: https://github.com/aws/aws-cdk/issues/11914
    CMD ["cp", "-r", "/asset-stage/.", "/asset-output/"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search