Cloudformation部署aws sdk for javascript

pel*_*can 5 node.js aws-cloudformation aws-sdk aws-sdk-js

看看用于Javascript的AWS sdk,看起来我们只能create stacks但我正在寻找到deploy堆栈的方式.我怎么用提供的sdk做到这一点; 这是他们目前拥有的:

cloudformation.createStack(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
Run Code Online (Sandbox Code Playgroud)

我希望这样的事情:

cloudformation.deployStack(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
Run Code Online (Sandbox Code Playgroud)

基本上,我想使用sdk而不是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
Run Code Online (Sandbox Code Playgroud)

这是因为我使用Linux并且可以将它放在shell脚本中,而我使用的大多数人都使用Windows而我不想使用Windows Batch,而是使用像npm这样的跨平台解决方案aws-sdk for javascript approach.

你将如何cloudformation.deployStack使用SDKNOTCLI执行?

Sea*_*ean 8

目前AWS SDK的JavaScript并没有现在有一个部署方法,但是,AWS CLI的部署命令是一个包装:

通过创建然后执行更改集来部署指定的 AWS CloudFormation 模板

考虑到这一点,我编写了以下代码:

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)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

注: changeSetType(在changeSetParams的一部分)需要被明确定义为无论是“创建或更新”。所以使用类似的东西:

const upsertParam = await CloudformationInstance.describeStacks(params, (err, data) => {
  if(err) return 'CREATE'
  return 'UPDATE'
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这很有帮助!!一些注意事项是 `waitFor` 应该在 `createChangeSet` 的回调中,否则,它会得到 'Resource not in the stack' 错误。此外,`waitFor('changeCreateComplete')` 应该传递 `ChangeSetName` 作为参数而不是 `StackName`。 (2认同)