Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation deploy aws sdk for javascript

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 ?

like image 406
pelican Avatar asked Sep 13 '18 00:09

pelican


People also ask

Does CloudFormation use AWS SDK?

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.

Can CloudFormation run a script?

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.

Does AWS support JavaScript?

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.

Is there an AWS SDK for JavaScript deployment?

The current AWS sdk for Javascript does not currently have a deploy method, however, the AWS CLI's deploy command is a wrapper:

How does CloudFormation work with Amazon Web Services APIs?

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.

What is the difference between AWS app runner and CloudFormation?

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.

How does CloudFormation update the stack instances in a set?

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>)


1 Answers

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'
}
like image 54
Sean Avatar answered Oct 16 '22 22:10

Sean