如何在ssm文档中提供表达式作为值

  • 本文关键字:表达式 ssm 文档 aws-ssm
  • 更新时间 :
  • 英文 :


我想使用SSM文档将服务器添加到ausosting组中,如果该组有n个实例在运行-我希望有(n+1(个实例。

由于这个堆栈是由cloudformation管理的,我只需要增加"DesiredCapacity"变量并更新堆栈。所以我用两个步骤创建了一个文档:

  1. 获取"DesiredCapacity"的当前值
  2. 更新值为"DesiredCapacity"+1的堆栈

我没有找到表达这个简单操作的方法,我想我做错了什么。。。

SSM文件:

schemaVersion: '0.3'
parameters:
cfnStack:
description: 'The cloudformation stack to be updated'
type: String
mainSteps:
- name: GetDesiredCount
action: 'aws:executeAwsApi'
inputs:
Service: cloudformation
Api: DescribeStacks
StackName: '{{ cfnStack }}'
outputs:
- Selector: '$.Stacks[0].Outputs.DesiredCapacity'
Type: String
Name: DesiredCapacity
- name: UpdateCloudFormationStack
action: 'aws:executeAwsApi'
inputs:
Service: cloudformation
Api: UpdateStack
StackName: '{{ cfnStack }}'
UsePreviousTemplate: true
Parameters:
- ParameterKey: WebServerCapacity
ParameterValue: 'GetDesiredCount.DesiredCapacity' + 1 ### ERROR
# ParameterValue: '{{GetDesiredCount.DesiredCapacity}}' + 1 ### ERROR (trying to concat STR to INT)
# ParameterValue: '{{ GetDesiredCount.DesiredCapacity + 1}}' ### ERROR

有一种方法可以使用python运行时在SSM文档中进行计算。额外的python步骤执行以下操作:

  1. Python运行时通过"InputPayload"属性获取变量
  2. 添加到事件对象的"current"(str(键
  3. 调用的python函数script_handler
  4. 使用事件['current']提取的'current'
  5. 将字符串转换为int并加1
  6. 以字符串形式返回具有"desired_capacity"键和值的字典
  7. 公开输出($.Payload.dedesired_capacity引用了返回字典的"desired_capacity"(
schemaVersion: '0.3'
parameters:
cfnStack:
description: 'The cloudformation stack to be updated'
type: String
mainSteps:
- name: GetDesiredCount
action: 'aws:executeAwsApi'
inputs:
Service: cloudformation
Api: DescribeStacks
StackName: '{{ cfnStack }}'
outputs:
- Selector: '$.Stacks[0].Outputs.DesiredCapacity'
Type: String
Name: DesiredCapacity

- name: Calculate
action: 'aws:executeScript'
inputs:
Runtime: python3.6
Handler: script_handler
Script: |-
def script_handler(events, context):
desired_capacity = int(events['current']) + 1
return {'desired_capacity': str(desired_capacity)}
InputPayload:
current: '{{ GetDesiredCount.DesiredCapacity }}'
outputs:
- Selector: $.Payload.desired_capacity
Type: String
Name: NewDesiredCapacity

- name: UpdateCloudFormationStack
action: 'aws:executeAwsApi'
inputs:
Service: cloudformation
Api: UpdateStack
StackName: '{{ cfnStack }}'
UsePreviousTemplate: true
Parameters:
- ParameterKey: WebServerCapacity
ParameterValue: '{{ Calculate.NewDesiredCapacity}}'

最新更新