Looking at the AWS sdk for Javascript, it appears we can only create stacks
but i'm looking of a way to deploy
a stack. How would I do that with the provided sdk; this is what they currently have:
cloudformation.createStack(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
I was hoping for something like this:
cloudformation.deployStack(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Basically, I would like to recreate this command using the sdk instead of the cli:
aws cloudformation deploy --template-file /path_to_template/template.json --stack-name my-new-stack --parameter-overrides Key1=Value1 Key2=Value2 --tags Key1=Value1 Key2=Value2
And this is because I use Linux and can put this in a shell script while most of the people I work with all use Windows and I don't want to use Windows Batch but instead a cross platform solution like npm and thus the aws-sdk for javascript approach
.
How would you perform the cloudformation.deployStack
using the SDK
and NOT
the CLI ?
AWS software development kits (SDKs) are available for many popular programming languages. Each SDK provides an API, code examples, and documentation that make it easier for developers to build applications in their preferred language.
For those learning AWS/AWS CLI, CloudFormation is a tool for building infrastructure with AWS. Here is a very simple document on how to use CloudFormation to build an AWS EC2 Linux instance and execute a bash script from CloudFormation against the newly created Linux instance.
The AWS SDK for JavaScript supports three runtimes: JavaScript for browser, Node. js for server, React Native for mobile development. It also supports cross-runtime: a service client package can be run on browsers, Node. js, and React-Native without code change.
The current AWS sdk for Javascript does not currently have a deploy method, however, the AWS CLI's deploy command is a wrapper:
If the registered extension calls any Amazon Web Services APIs, you must create an IAM execution rolethat includes the necessary permissions to call those Amazon Web Services APIs, and provision that execution role in your account. CloudFormation then assumes that execution role to provide your extension with the appropriate credentials.
AWS App Runner is an AWS service that provides a fast, simple, and cost-effective way to deploy straight from source code or a container image directly to a scalable and secure web application in the AWS Cloud. CloudFormation is the IaaC tool you can automate the infrastructure creation on AWS.
If the stack set update does not include changes to the template or parameters, CloudFormation updates the stack instances in the specified accounts and Amazon Web Services Regions, while leaving all other stack instances with their existing stack instance status. Regions— (Array<String>)
The current AWS sdk for Javascript does not currently have a deploy method, however, the AWS CLI's deploy command is a wrapper:
Deploys the specified AWS CloudFormation template by creating and then executing a change set
With that in mind, I wrote the following code:
const CloudformationInstance = new Cloudformation(accessParams)
CloudformationInstance.createChangeSet(changeSetParams, (err, data) => {
if (err) throw new Error(err, err.stack)
console.info('Succesfully created the ChangeSet: ', data)
CloudformationInstance.waitFor('changeSetCreateComplete', {ChangeSetName: config.changeSetName}, (err, data) => {
if (err) throw new Error(err, err.stack)
const { StackName } = data.Stacks[0]
CloudformationInstance.executeChangeSet({ StackName, ChangeSetName }, (err, data) => {
if (err) throw new Error(err, err.stack)
console.info('Succesfully finished creating the set: ', data)
})
})
})
Note: changeSetType(part of the changeSetParams) needs to be explicitly defined as either "Create or Update". So using something like:
const upsertParam = await CloudformationInstance.describeStacks(params, (err, data) => {
if(err) return 'CREATE'
return 'UPDATE'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With