Skip to content

Commit

Permalink
feat: update alpha modules to sdk v3 (#25895)
Browse files Browse the repository at this point in the history
Updates alpha modules with custom resources to use NodeJS 18 and v3 of the AWS SDK. Since these CRs are pretty self-contained and safe to update, their handler code and runtime versions can be updated without any breakage for users.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
MrArnoldPalmer authored Jun 30, 2023
1 parent bd86993 commit 0a4140e
Show file tree
Hide file tree
Showing 37 changed files with 31,661 additions and 1,522 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// aws-sdk available at runtime for lambdas
// eslint-disable-next-line import/no-extraneous-dependencies
import { Amplify, S3 } from 'aws-sdk';
import { Amplify } from '@aws-sdk/client-amplify';
import { S3, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { AmplifyJobId, IsCompleteResponse, ResourceEvent, ResourceHandler } from './common';

export interface AmplifyAssetDeploymentProps {
Expand Down Expand Up @@ -38,8 +38,7 @@ export class AmplifyAssetDeploymentHandler extends ResourceHandler {
appId: this.props.AppId,
branchName: this.props.BranchName,
maxResults: 1,
})
.promise();
});

if (
jobs.jobSummaries &&
Expand All @@ -49,22 +48,22 @@ export class AmplifyAssetDeploymentHandler extends ResourceHandler {
}

// Create a pre-signed get URL of the asset so Amplify can retrieve it.
const assetUrl = this.s3.getSignedUrl('getObject', {
const command = new GetObjectCommand({
Bucket: this.props.S3BucketName,
Key: this.props.S3ObjectKey,
});
const assetUrl = await getSignedUrl(this.s3, command);

// Deploy the asset to Amplify.
const deployment = await this.amplify
.startDeployment({
appId: this.props.AppId,
branchName: this.props.BranchName,
sourceUrl: assetUrl,
})
.promise();
});

return {
AmplifyJobId: deployment.jobSummary.jobId,
AmplifyJobId: deployment.jobSummary?.jobId,
};
}

Expand Down Expand Up @@ -110,19 +109,18 @@ export class AmplifyAssetDeploymentHandler extends ResourceHandler {
appId: this.props.AppId,
branchName: this.props.BranchName,
jobId: jobId,
})
.promise();
});

if (job.job.summary.status === 'SUCCEED') {
if (job.job?.summary?.status === 'SUCCEED') {
return {
IsComplete: true,
Data: {
JobId: jobId,
Status: job.job.summary.status,
},
};
} if (job.job.summary.status === 'FAILED' || job.job.summary.status === 'CANCELLED') {
throw new Error(`Amplify job failed with status: ${job.job.summary.status}`);
} if (job.job?.summary?.status === 'FAILED' || job.job?.summary?.status === 'CANCELLED') {
throw new Error(`Amplify job failed with status: ${job.job?.summary?.status}`);
} else {
return {
IsComplete: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { IsCompleteResponse } from 'aws-cdk-lib/custom-resources/lib/provider-framework/types';
// aws-sdk available at runtime for lambdas
// @aws-sdk/* modules available at runtime for lambdas >= Node18
// eslint-disable-next-line import/no-extraneous-dependencies
import { Amplify, S3, config } from 'aws-sdk';
import { Amplify } from '@aws-sdk/client-amplify';
import { S3 } from '@aws-sdk/client-s3';
import { ResourceEvent } from './common';
import { AmplifyAssetDeploymentHandler } from './handler';

const AMPLIFY_ASSET_DEPLOYMENT_RESOURCE_TYPE = 'Custom::AmplifyAssetDeployment';

config.logger = console;

const amplify = new Amplify();
const s3 = new S3({ signatureVersion: 'v4' });
const sdkConfig = { logger: console };
const amplify = new Amplify(sdkConfig);
const s3 = new S3(sdkConfig);

export async function onEvent(event: ResourceEvent) {
const provider = createResourceHandler(event);
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/lib/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CfnBranch } from 'aws-cdk-lib/aws-amplify';
import { IApp } from './app';
import { BasicAuth } from './basic-auth';
import { renderEnvironmentVariables } from './utils';
import { Runtime } from 'aws-cdk-lib/aws-lambda';

/**
* A branch
Expand Down Expand Up @@ -248,6 +249,7 @@ class AmplifyAssetDeploymentProvider extends NestedStack {
],
}),
],
runtime: Runtime.NODEJS_18_X,
},
);

Expand All @@ -266,6 +268,7 @@ class AmplifyAssetDeploymentProvider extends NestedStack {
actions: ['amplify:GetJob*'],
}),
],
runtime: Runtime.NODEJS_18_X,
},
);

Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@aws-sdk/client-amplify": "^3.347.0",
"@aws-sdk/client-s3": "^3.347.0",
"@aws-sdk/s3-request-presigner": "^3.347.0",
"@types/jest": "^29.5.1",
"aws-sdk": "^2.1379.0",
"aws-cdk-lib": "0.0.0",
"constructs": "^10.0.0"
},
Expand Down
Loading

0 comments on commit 0a4140e

Please sign in to comment.