Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset support in CI/CD pipelines #1312

Closed
phstc opened this issue Dec 10, 2018 · 20 comments
Closed

Asset support in CI/CD pipelines #1312

phstc opened this issue Dec 10, 2018 · 20 comments
Assignees
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. in-progress This issue is being actively worked on. package/tools Related to AWS CDK Tools or CLI

Comments

@phstc
Copy link
Contributor

phstc commented Dec 10, 2018

Hi

I'm trying to follow these steps CodePipeline Actions for CloudFormation for building with CFN, but I'm have a hard time to make templatePath work.

CodeBuild calls cdk synth (and outputs template.yaml) then I set as the templatePath as follows:

const project = new codebuild.PipelineProject(this, 'Project', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0
  },
  buildSpec: {
    version: '0.2',
    phases: {
      install: {
        commands: [
          '...'
        ]
      },
      build: {
        commands: [
          'cdk synth > ../template.yaml'
        ]
      }
    },
    artifacts: {
      files: ['template.yaml']
    }
  }
})

// ...

const pipeline = new codepipeline.Pipeline(this, 'CodePipeline', {})

const build = new codebuild.PipelineBuildAction(this, 'CodeBuild', {
  stage: pipeline.addStage('Build'),
  project,
  outputArtifactName: 'Build'
})

new cfn.PipelineCreateReplaceChangeSetAction(prodStage, 'PrepareChanges', {
  stage: prodStage,
  stackName,
  changeSetName,
  adminPermissions: true,
  templatePath: build.outputArtifact.atPath('template.yaml')
})

But I keep getting this error on the PrepareChanges stage:

image

I checked the artifact generated in the build stage and the template.yaml is in there.

Just as a random attempt, I've also tried to commit the template.yaml and use directly from source instead of build:

new codepipeline.GitHubSourceAction(this, 'GitHubSource', {
  stage: pipeline.addStage('Source'),
  owner: 'project',
  repo: 'repo',
  branch: 'master',
  oauthToken: new Secret(oauth.value),
  outputArtifactName: 'Source'
})

new cfn.PipelineCreateReplaceChangeSetAction(prodStage, 'PrepareChanges', {
  stage: prodStage,
  stackName,
  changeSetName,
  adminPermissions: true,
  templatePath: source.outputArtifact.atPath('template.yaml')
})

But it didn't work, I got the same error.

Any ideas?

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 10, 2018

I've asked some people more knowledgeable to take a look, but from the error message it looks like the project you're trying to deploy is using assets.

This might not be supported yet.

@phstc
Copy link
Contributor Author

phstc commented Dec 10, 2018

Hi @rix0rrr

Thanks for the quick reply. WDYM by assets? This project has a static website and a lambda.

const websiteBucket = new s3.Bucket(parent, 'bucket', {
    websiteIndexDocument: 'index.html',
    publicReadAccess: true
})

new s3deploy.BucketDeployment(parent, 'bucket-deployment', {
  source: s3deploy.Source.asset('../web/build'),
  destinationBucket: websiteBucket
})

const fn = new lambda.Function(parent, 'function', {
  runtime: new lambda.Runtime('ruby2.5'),
  handler: 'lambda.handler',
  code: lambda.Code.asset('../app')
})

cdk synth and cdk deploy works fine, also if I download the artifact on S3 and unzip it, the template.yaml is in there.

I'm wondering if the problem is with one of these PipelineCreateReplaceChangeSetAction or templatePath: build.outputArtifact.atPath('template.yaml').

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 10, 2018

Hi @phstc,

This is what I mean by assets:

new s3deploy.BucketDeployment(parent, 'bucket-deployment', {
  source: s3deploy.Source.asset('../web/build'),
                          ^^^^^  // this
  destinationBucket: websiteBucket
})

const fn = new lambda.Function(parent, 'function', {
  runtime: new lambda.Runtime('ruby2.5'),
  handler: 'lambda.handler',
  code: lambda.Code.asset('../app')
                    ^^^^^ // and this
})

Any time you're referencing files on your local file system, we're referring to that as "assets".

It seems my guess that there is no CI/CD asset support yet is true. One of my team members who knows more about this will chip in with more information shortly.

@phstc
Copy link
Contributor Author

phstc commented Dec 10, 2018

Thanks again @rix0rrr

Confirmed. When I remove my bucket and lambda PipelineCreateReplaceChangeSetAction works.

@rix0rrr rix0rrr changed the title PipelineCreateReplaceChangeSetAction/templatePath is not working Asset support in CI/CD pipelines Dec 10, 2018
@rix0rrr rix0rrr added feature-request A feature should be added or improved. package/tools Related to AWS CDK Tools or CLI @aws-cdk/aws-codepipeline Related to AWS CodePipeline labels Dec 10, 2018
@rix0rrr rix0rrr added the gap label Jan 4, 2019
@justinThompson
Copy link

Is the workaround to use inline lambda code for the time being?

@justinThompson
Copy link

If anyone else runs into this, I used this as a workaround:

new lambda.Function(this, 'function', {
      runtime: lambda.Runtime.NodeJS810,
      handler: 'index.handler',
      code: lambda.Code.inline(this.localAsset(path.join(__dirname, 'index.js'))),
});
localAsset(path: string) {
    return fs.readFileSync(path, 'utf8');
}

@joshrp
Copy link
Contributor

joshrp commented Jul 25, 2019

So as far as I can tell this means any CDK project using decently large Lambdas can't be deployed through a proper CD pipeline? It has to be a full cdk deploy or nothing?

If there was a step between the lines here: https://github.com/aws/aws-cdk/blob/master/packages/aws-cdk/lib/api/deploy-stack.ts#L91 for creating a ChangeSet and Executing it, I belive it could work. Everything would have been uploaded and parameters would be set, but it wouldn't be released until the Execute step. Which could be manual, CodePipeline or CLI.

@BDQ
Copy link
Contributor

BDQ commented Jul 25, 2019

@joshrp we're using cdk deploy within a CodePipeline pretty successfully, we're using the -e option to only deploy the lambda stack, and it's been working rock solid for us so far.

@joshrp
Copy link
Contributor

joshrp commented Jul 25, 2019

I hadn't see the -e, that might be a partial solution, thanks. But I don't think I was clear before, by a proper CD setup I mean I can run a build, produce an artifact and then deploy it later. Mostly so I can have a step between build and a production deploy and have artifacts I can rollback to.

If I have to rely on cdk deploy then I'm running the whole build again in production, and whatever is running that build also has to have access to deploy to production.

Ideally cdk would create a CloudFormation ChangeSet, and then stop, allowing me to approve or deny, delay or inspect it.

@jonny-rimek
Copy link

I'm running into the same issue, just found the issue now

@eladb
Copy link
Contributor

eladb commented Jul 29, 2019

Please see #3437 for details on how we plan to support this

@LiveJay
Copy link

LiveJay commented Aug 9, 2019

If anyone else runs into this, I used this as a workaround:

new lambda.Function(this, 'function', {
      runtime: lambda.Runtime.NodeJS810,
      handler: 'index.handler',
      code: lambda.Code.inline(this.localAsset(path.join(__dirname, 'index.js'))),
});
localAsset(path: string) {
    return fs.readFileSync(path, 'utf8');
}

This only handles the Lambda. Is there currently a workaround for the BucketDeployment asset?

@jonny-rimek
Copy link

jonny-rimek commented Aug 10, 2019

@LiveJay this is how you can reference the bucket asset, it works fine for me, but I don't really get how I should handle multiple lambdas in 1 stack.

@skinny85 skinny85 self-assigned this Aug 12, 2019
@SomayaB SomayaB added the in-progress This issue is being actively worked on. label Nov 13, 2019
@RomainMuller RomainMuller removed their assignment Jan 24, 2020
eladb pushed a commit that referenced this issue Jan 26, 2020
This PR includes the RFC for supporting CI/CD for CDK apps of any complexity.

It also includes the RFC for cdk-assets which is a tool for publishing CDK assets, and is part of the CI/CD solution.

Tracking issue: aws/aws-cdk-rfcs#49
Addresses: #1312
@skinny85 skinny85 added the effort/large Large work item – several weeks of effort label Feb 6, 2020
@saltman424
Copy link
Contributor

Any updates on when this is expected to be supported?

@ottokruse
Copy link

In a current project we're using this gist as workaround while waiting for native CDK support: https://gist.github.com/ottokruse/41694de3831d6bfe9080743b352aa2fe

It's a script to publish CDK assets (e.g. Lambda function code) to S3 and generate parameter files, so you can combine cdk synth with CloudFormation deployments. This is essentially the equivalent of 'sam package' but then for CDK.

@mikestopcontinues
Copy link

@BDQ How do you handle cdk permissions within codebuild?

@BDQ
Copy link
Contributor

BDQ commented Aug 7, 2020

@mikestopcontinues we add the required permissions codebuild job's Role policy:

const deploy = new codebuild.PipelineProject(stack, "dply", {...})

 deploy.addToRolePolicy(
    new iam.PolicyStatement({
      resources: [...],
      actions: [...]
   })
)

@mikestopcontinues
Copy link

@BDQ Thanks!

@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 12, 2020

This has been implemented as CDK Pipelines.

@rix0rrr rix0rrr closed this as completed Aug 12, 2020
@mikestopcontinues
Copy link

@rix0rrr Are there plans to support report groups in pipelines? It's why I haven't switched to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. in-progress This issue is being actively worked on. package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests