'Trigger an aws-amplify build via aws-cdk

I am creating a aws-amplify app via aws-cdk and everything works fine except it doesn't start a build automatically. If I do a git commit (I have enabled continuous deploys) it will build and run just fine. But on a new aws account with a cdk deploy I have to start the first commit manually...



Solution 1:[1]

I had this issue as well while working on the deployment of an amplify hosted application.

After going through the AWS CLI documentation for Amplify, I found the start-job command. This command allows you to start an amplify job for a specific branch.

You can then create an AwsCustomResource that makes an SDK call to start-job.

This is what I ended up with.

const build_trigger = new customResource.AwsCustomResource(this, 'triggerAppBuild', {
    policy: customResource.AwsCustomResourcePolicy.fromSdkCalls({
        resources: customResource.AwsCustomResourcePolicy.ANY_RESOURCE
    }),
    onCreate: {
        service: 'Amplify',
        action: 'startJob',
        physicalResourceId: customResource.PhysicalResourceId.of('app-build-trigger'),
        parameters: {
            appId: amplifyApp.appId,
            branchName: master.branchName,
            jobType: 'RELEASE',
            jobReason: 'Auto Start build',
        }
    },
});

Note that you should replace amplifyApp.apiId and master.branchName with your own app ID and branch name. I am using the amplify-alpha, so you may need to get the App ID and branch name another way.

Solution 2:[2]

Amplify app builds are triggered by push events. Use a CDK Custom Resource or its simpler cousin Trigger to generate a push event during the CDK stack creation.

For a github repo, for instance, your Trigger construct's lambda would call the Github webhook test API, which will trigger the hook with the latest push to the current repository. Both Custom Resources and Triggers can be configured to run only on stack creation. Remember to give your lambda any necessary repo credentials (e.g. set environment variables via a secret).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bobveringa
Solution 2 fedonev