Created
June 16, 2018 19:52
-
-
Save y13i/baa6319dd9737b72a7d49dcd71821531 to your computer and use it in GitHub Desktop.
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
const { config, STS } = require("aws-sdk"); | |
const { execSync } = require("child_process"); | |
const sts = new STS(); | |
if (!config.region) { | |
console.log("Set `$AWS_REGION` environment variable."); | |
process.exit(1); | |
} | |
let Bucket; | |
sts | |
.getCallerIdentity() | |
.promise() | |
.then(callerIdentity => { | |
process.env["AWS_DEFAULT_REGION"] = config.region; | |
Bucket = `cfn.${callerIdentity.Account}.${config.region}`; | |
const packageCommand = [ | |
"aws", | |
"cloudformation", | |
"package", | |
"--template-file", | |
"template.cfn.json", | |
"--s3-bucket", | |
Bucket, | |
"--output-template-file", | |
".cfn/packaged.json", | |
"--use-json" | |
].join(" "); | |
console.log(packageCommand); | |
execSync(packageCommand, { stdio: "inherit" }); | |
const deployCommand = [ | |
"aws", | |
"cloudformation", | |
"deploy", | |
"--template-file", | |
".cfn/packaged.json", | |
"--capabilities", | |
"CAPABILITY_IAM", | |
"--stack-name", | |
require("../package.json").name | |
].join(" "); | |
console.log(deployCommand); | |
execSync(deployCommand, { stdio: "inherit" }); | |
}) | |
.catch(error => { | |
console.log(error); | |
process.exit(1); | |
}); |
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
const { config, STS, S3 } = require("aws-sdk"); | |
const sts = new STS(); | |
const s3 = new S3(); | |
if (!config.region) { | |
console.log("Set `$AWS_REGION` environment variable."); | |
process.exit(1); | |
} | |
let Bucket; | |
Promise.resolve() | |
.then(() => { | |
return sts.getCallerIdentity().promise(); | |
}) | |
.then(callerIdentity => { | |
Bucket = `cfn.${callerIdentity.Account}.${config.region}`; | |
return s3.getBucketLocation({ Bucket }).promise(); | |
}) | |
.then(getBucketLocationOutput => { | |
const region = getBucketLocationOutput.LocationConstraint; | |
if ( | |
region === config.region || | |
(config.region === "us-east-1" && region === "") | |
) { | |
process.exit(0); | |
} else { | |
console.log( | |
`Bucket \`${Bucket}\` is not in region \`${config.region}\`.` | |
); | |
process.exit(1); | |
} | |
}) | |
.catch(error => { | |
if (error.name === "NoSuchBucket") { | |
return s3 | |
.createBucket({ | |
Bucket, | |
CreateBucketConfiguration: | |
config.region === "us-east-1" | |
? undefined | |
: { | |
LocationConstraint: config.region | |
} | |
}) | |
.promise(); | |
} else { | |
console.log(error); | |
process.exit(1); | |
} | |
}) | |
.then(createBucketOutput => { | |
console.log("Bucket created", createBucketOutput); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment