-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy-webapp.js
86 lines (71 loc) · 2.82 KB
/
deploy-webapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: LicenseRef-.amazon.com.-AmznSL-1.0
// Licensed under the Amazon Software License http://aws.amazon.com/asl/
const {execSync} = require('child_process');
const path = require('path');
function getArgument( name, defaultValue ) {
let matched = false;
for ( let arg of process.argv ) {
console.log( arg );
if ( arg === name ) {
matched = true;
continue;
}
if ( matched ) {
return arg;
}
}
return defaultValue;
}
/**
* This file fishes out the bucket name created using the Cloud Formation template
* and then runs a command line script that synchronizes the contents of ./web
* into that bucket, setting permissions to public
*/
(function() {
let askStates;
try {
askStates = require( path.resolve('.', '.ask', 'ask-states.json') );
} catch (err) {
throw new Error("failed to load ask-states.json Have you successfully executed ask deploy yet?");
}
const profile = askStates.profiles.default;
if ( !profile ) {
throw new Error("failed to obtain profile object. Did you specify the right one?");
}
const infrastructure = profile.skillInfrastructure;
if ( !infrastructure ) {
throw new Error("failed to obtain skillInfrastructure object. Have you successfully executed ask deploy yet?");
}
const cfn = infrastructure['@ask-cli/cfn-deployer'];
if ( !cfn ) {
throw new Error("failed to obtain the @ask-cli/cfn-deployer object. Is this skill configured to use CloudFront?");
}
let outputs;
try {
outputs = cfn.deployState.default.outputs;
} catch {}
if ( !outputs ) {
throw new Error("No CloudFormation outputs found, check the developer console for potential CloudFormation errors.");
}
let bucketName;
outputs.forEach(output => {
if ( output.OutputKey === 'WebAppBucketName' ) {
bucketName = output.OutputValue;
}
});
if (!bucketName) {
throw new Error("Did not find the outputs WebAppBucketName. Did you modify the CloudFormation file?")
}
const profileName = getArgument( '-p', 'default' );
const sourceDirectory = './web';
console.log( `==================== Deploy S3 Web App ====================` );
console.log( `found bucket from CloudFormation: ${bucketName}, starting deployment to AWS profile ${profileName}` );
try {
execSync(`aws s3 sync ${sourceDirectory} s3://${bucketName} --cache-control "no-cache" --acl public-read --profile ${profileName}`, {stdio:'inherit'});
} catch (err) {
console.error(`s3 deployment failed, see output above for details. If aws cli could not locate credentials, do you have a valid default profile? If not, did you mean to run this script with the -p flag to specify a non default one?`);
return;
}
console.log( `s3 deployment complete`);
})()