代码之家  ›  专栏  ›  技术社区  ›  pelican

Cloudformation部署用于javascript的aws sdk

  •  5
  • pelican  · 技术社区  · 7 年前

    看看AWS的Javascript 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
    });
    

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

    基本上,我希望使用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
    

    aws-sdk for javascript approach .

    cloudformation.deployStack 使用 SDK NOT CLI?

    1 回复  |  直到 7 年前
        1
  •  8
  •   Sean    7 年前

    当前用于Javascript的AWS sdk不支持 目前有一个部署方法,但是AWS CLI的 deploy 命令是一个包装器:

    通过创建并执行更改集来部署指定的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)
        })
      })
    })
    

    注: changeSetType(changeSetParams的一部分)需要显式定义为 “创建或更新”。因此,使用类似于:

    const upsertParam = await CloudformationInstance.describeStacks(params, (err, data) => {
      if(err) return 'CREATE'
      return 'UPDATE'
    }
    
    推荐文章