-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(amplify): Add Amplify asset deployment resource #16922
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd97d74
feat(amplify): Add Amplify asset deployment resource
samkio a0769bb
Merge branch 'master' into master
MrArnoldPalmer 9b4d672
Merge branch 'master' into master
samkio 92eee7d
Fix package.json typo
samkio d75d551
Merge remote-tracking branch 'upstream/master'
samkio 333cfbf
Make deployment part of branch constructor
samkio c59b290
Fix integ test
samkio dd76559
Fix unit test
samkio ddfd773
Merge branch 'master' into master
samkio f8cae0a
Update README
samkio 11ce555
Merge branch 'master' of github.com:samkio/aws-cdk
samkio ebb20bd
Merge branch 'master' into master
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/@aws-cdk/aws-amplify/lib/asset-deployment-handler/common.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
export interface AmplifyJobId { | ||
/** | ||
* If this field is included in an event passed to "IsComplete", it means we | ||
* initiated an Amplify deployment that should be monitored using | ||
* amplify:GetJob | ||
*/ | ||
AmplifyJobId?: string; | ||
} | ||
|
||
export type ResourceEvent = AWSLambda.CloudFormationCustomResourceEvent & AmplifyJobId; | ||
|
||
export interface IsCompleteResponse { | ||
/** | ||
* Indicates if the resource operation is complete or should we retry. | ||
*/ | ||
readonly IsComplete: boolean; | ||
|
||
/** | ||
* Additional/changes to resource attributes. | ||
*/ | ||
readonly Data?: { [name: string]: any }; | ||
}; | ||
|
||
export abstract class ResourceHandler { | ||
protected readonly requestId: string; | ||
protected readonly logicalResourceId: string; | ||
protected readonly requestType: 'Create' | 'Update' | 'Delete'; | ||
protected readonly physicalResourceId?: string; | ||
protected readonly event: ResourceEvent; | ||
|
||
constructor(event: ResourceEvent) { | ||
this.requestType = event.RequestType; | ||
this.requestId = event.RequestId; | ||
this.logicalResourceId = event.LogicalResourceId; | ||
this.physicalResourceId = (event as any).PhysicalResourceId; | ||
this.event = event; | ||
} | ||
|
||
public onEvent() { | ||
switch (this.requestType) { | ||
case 'Create': | ||
return this.onCreate(); | ||
case 'Update': | ||
return this.onUpdate(); | ||
case 'Delete': | ||
return this.onDelete(); | ||
} | ||
|
||
throw new Error(`Invalid request type ${this.requestType}`); | ||
} | ||
|
||
public isComplete() { | ||
switch (this.requestType) { | ||
case 'Create': | ||
return this.isCreateComplete(); | ||
case 'Update': | ||
return this.isUpdateComplete(); | ||
case 'Delete': | ||
return this.isDeleteComplete(); | ||
} | ||
|
||
throw new Error(`Invalid request type ${this.requestType}`); | ||
} | ||
|
||
protected log(x: any) { | ||
// eslint-disable-next-line no-console | ||
console.log(JSON.stringify(x, undefined, 2)); | ||
} | ||
|
||
protected abstract async onCreate(): Promise<AmplifyJobId>; | ||
protected abstract async onDelete(): Promise<void>; | ||
protected abstract async onUpdate(): Promise<AmplifyJobId>; | ||
protected abstract async isCreateComplete(): Promise<IsCompleteResponse>; | ||
protected abstract async isDeleteComplete(): Promise<IsCompleteResponse>; | ||
protected abstract async isUpdateComplete(): Promise<IsCompleteResponse>; | ||
} |
136 changes: 136 additions & 0 deletions
136
packages/@aws-cdk/aws-amplify/lib/asset-deployment-handler/handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// aws-sdk available at runtime for lambdas | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Amplify, S3 } from 'aws-sdk'; | ||
import { AmplifyJobId, IsCompleteResponse, ResourceEvent, ResourceHandler } from './common'; | ||
|
||
export interface AmplifyAssetDeploymentProps { | ||
AppId: string; | ||
BranchName: string; | ||
S3BucketName: string; | ||
S3ObjectKey: string; | ||
TimeoutSeconds: number; | ||
} | ||
|
||
export class AmplifyAssetDeploymentHandler extends ResourceHandler { | ||
private readonly props: AmplifyAssetDeploymentProps; | ||
protected readonly amplify: Amplify; | ||
protected readonly s3: S3; | ||
|
||
constructor(amplify: Amplify, s3: S3, event: ResourceEvent) { | ||
super(event); | ||
|
||
this.props = parseProps(this.event.ResourceProperties); | ||
this.amplify = amplify; | ||
this.s3 = s3; | ||
} | ||
|
||
// ------ | ||
// CREATE | ||
// ------ | ||
|
||
protected async onCreate(): Promise<AmplifyJobId> { | ||
// eslint-disable-next-line no-console | ||
console.log('deploying to Amplify with options:', JSON.stringify(this.props, undefined, 2)); | ||
|
||
// Verify no jobs are currently running. | ||
const jobs = await this.amplify | ||
.listJobs({ | ||
appId: this.props.AppId, | ||
branchName: this.props.BranchName, | ||
maxResults: 1, | ||
}) | ||
.promise(); | ||
|
||
if ( | ||
jobs.jobSummaries && | ||
jobs.jobSummaries.find(summary => summary.status === 'PENDING') | ||
) { | ||
return Promise.reject('Amplify job already running. Aborting deployment.'); | ||
MrArnoldPalmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Create a pre-signed get URL of the asset so Amplify can retrieve it. | ||
const assetUrl = this.s3.getSignedUrl('getObject', { | ||
Bucket: this.props.S3BucketName, | ||
Key: this.props.S3ObjectKey, | ||
}); | ||
|
||
// 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, | ||
}; | ||
} | ||
|
||
protected async isCreateComplete() { | ||
return this.isActive(this.event.AmplifyJobId); | ||
} | ||
|
||
// ------ | ||
// DELETE | ||
// ------ | ||
|
||
protected async onDelete(): Promise<void> { | ||
// We can't delete this resource as it's a deployment. | ||
return; | ||
} | ||
|
||
protected async isDeleteComplete(): Promise<IsCompleteResponse> { | ||
// We can't delete this resource as it's a deployment. | ||
return { | ||
IsComplete: true, | ||
}; | ||
} | ||
|
||
// ------ | ||
// UPDATE | ||
// ------ | ||
|
||
protected async onUpdate() { | ||
return this.onCreate(); | ||
} | ||
|
||
protected async isUpdateComplete() { | ||
return this.isActive(this.event.AmplifyJobId); | ||
} | ||
|
||
private async isActive(jobId?: string): Promise<IsCompleteResponse> { | ||
if (!jobId) { | ||
throw new Error('Unable to determine Amplify job status without job id'); | ||
} | ||
|
||
const job = await this.amplify | ||
.getJob({ | ||
appId: this.props.AppId, | ||
branchName: this.props.BranchName, | ||
jobId: jobId, | ||
}) | ||
.promise(); | ||
|
||
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}`); | ||
} else { | ||
return { | ||
IsComplete: false, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
function parseProps(props: any): AmplifyAssetDeploymentProps { | ||
return props; | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/@aws-cdk/aws-amplify/lib/asset-deployment-handler/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { IsCompleteResponse } from '@aws-cdk/custom-resources/lib/provider-framework/types'; | ||
// aws-sdk available at runtime for lambdas | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Amplify, S3, config } from 'aws-sdk'; | ||
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' }); | ||
|
||
export async function onEvent(event: ResourceEvent) { | ||
const provider = createResourceHandler(event); | ||
return provider.onEvent(); | ||
} | ||
|
||
export async function isComplete( | ||
event: ResourceEvent, | ||
): Promise<IsCompleteResponse> { | ||
const provider = createResourceHandler(event); | ||
return provider.isComplete(); | ||
} | ||
|
||
function createResourceHandler(event: ResourceEvent) { | ||
switch (event.ResourceType) { | ||
case AMPLIFY_ASSET_DEPLOYMENT_RESOURCE_TYPE: | ||
return new AmplifyAssetDeploymentHandler(amplify, s3, event); | ||
default: | ||
throw new Error(`Unsupported resource type "${event.ResourceType}"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I suggested this
addAssetDeployment()
but thinking more about this, can we add more than one asset deployment to a branch? I don't think so. So maybe it should be moved to a construction prop inBranch
/option inaddBranch()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good; amended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to update the README for the latest change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done