aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.35k stars 3.77k forks source link

aws_codebuild: Add possibility to schedule batch build #22841

Open rafzei opened 1 year ago

rafzei commented 1 year ago

Describe the feature

I would like to schedule AWS Codebuild batch build using aws_events.Rule target.

The following code is scheduling standard build even if test_project has batch builds enabled.

aws_events.Rule(
    self,
   "schedule_rule",
    schedule=aws_events.Schedule.cron(cron_schedule),
    targets=[
        aws_events_targets.CodeBuildProject(
            project=test_project,
            event=aws_events.RuleTargetInput.from_object(
                rule_target_input
            ),
        ),
    ],
)

Use Case

I need this to run scheduled batch builds

Proposed Solution

aws_events.Rule(
    self,
   "schedule_rule",
    schedule=aws_events.Schedule.cron(cron_schedule),
    targets=[
        aws_events_targets.CodeBuildProject(
            project=test_project,
            event=aws_events.RuleTargetInput.from_object(
                rule_target_input
            ),
            batch_build=True
        ),
    ],
)

test_project has to have batch_builds enabled (test_project.enable_batch_builds()).

Other Information

Currently, I see two ways to run codebuild batch builds:

  1. CDK Codebuild source: webhook_triggers_batch_build=True docs link

  2. AWS CLI: aws codebuild start-build-batch --project-name <project-name> docs link It could be used in lambda as a workaround.

Acknowledgements

CDK version used

2.50.0 (build 4c11af6)

Environment details (OS name and version, etc.)

Ubuntu 22.04

indrora commented 1 year ago

We do have support in CodeBuild for batch builds. You'll need to declare that batch builds are possible: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codebuild-readme.html#batch-builds

However, I don't think that's the answer you're looking for. Do you have an example of achieving this using CloudFormation?

github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

rafzei commented 1 year ago

We do have support in CodeBuild for batch builds. You'll need to declare that batch builds are possible: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codebuild-readme.html#batch-builds

However, I don't think that's the answer you're looking for. Do you have an example of achieving this using CloudFormation?

No, I didn't find a way to do this directly.

rafzei commented 1 year ago

@TheRealAmazonKendra any update?

vandonr-amz commented 1 year ago

I have the same need, but I think this issue should be raised with either the CodeBuild and/or EventBridge teams, because even outside of CDK I didn't find a way to have a Rule trigger a batch CodeBuild Project. I think this capability is just not present in the current product offering, which is sad.

I was thinking about using a lambda as a workaround like you mentioned, but it's killing a fly with a hammer...

TheRealAmazonKendra commented 1 year ago

You could likely build a custom resource to do this, but as it is not currently possible in CloudFormation, this is not something we can support at this time.

Makeshift commented 5 months ago

This is now possible in CloudFormation and the usual escape-hatch workaround can be used to do it in CDK for the time being. Just note that you will need to convert the props CfnProject.ProjectBuildBatchConfigProperty into CamelCase for it to work in Cloudformation, since the generated CDK code for the L1 construct puts it into camelCase:

function addBatchBuilds(project: Project, batchBuildConfig: CfnProject.ProjectBuildBatchConfigProperty) {
const cfnProject = project.node.defaultChild as CfnProject
cfnProject.addOverride('Properties.BuildBatchConfig', objectKeysToPascalCase({
  serviceRole: this.enableBatchBuilds()?.role?.roleArn,
    ...typeof batchBuildConfig !== 'boolean' ? batchBuildConfig : {}
}))