如何在 stepfunction 参数中使用 serverless 环境变量

时间:2021-03-18 05:03:25

标签: aws-serverless aws-step-functions

我有一个在参数部分使用硬编码日期的查询。相反,我想将它们作为环境变量传递。有关如何参数化 QueryString 参数的任何建议?

service: service-name

frameworkVersion: '2'

provider:
  name: aws
  runtime: go1.x
  lambdaHashingVersion: 20201221
  stage: ${opt:stage, self:custom.defaultStage}
region: us-east-1
  tags: ${self:custom.tagsObject}
  logRetentionInDays: 1
  timeout: 10
  deploymentBucket: lambda-repository
  memorySize: 128
  tracing:
    lambda: true

plugins:
  - serverless-step-functions

configValidationMode: error

stepFunctions:
  stateMachines:
    callAthena:
      name: datasorting-dev
      type: STANDARD
      role: ${self:custom.datasorting.${self:provider.stage}.iam}
      definition:
        Comment: "Data Refersh"
        StartAt: Refresh Data
        States:
          Refresh Data:
            Type: Task
            Resource: arn:aws:states:::athena:startQueryExecution.sync
            Parameters:
              QueryString: >-
                ALTER TABLE table.raw_data ADD IF NOT EXISTS
                PARTITION  (YEAR=2021, MONTH=02, DAY=15, hour=00)
              WorkGroup: primary
              ResultConfiguration:
                OutputLocation: s3://output/location
            End: true  
    

1 个答案:

答案 0 :(得分:0)

您可以替换 serverless.yml 中包含在 ${} 方括号中的任何值,

无服务器框架变量指南: https://www.serverless.com/framework/docs/providers/aws/guide/variables/

例如,您可以创建一个 custom: 部分来查找环境变量,如果它们不存在,您可以使用默认值:

service: service-name

frameworkVersion: '2'

custom:
  year: ${env:YEAR, 'default-year'}
  month: ${env:MONTH, 'default-month'}
  day: ${env:DAY, 'default-day'}
  hour: ${env:HOUR, 'default-hour'}

stepFunctions:
  stateMachines:
    callAthena:
      ...
      Parameters:
        QueryString: >-
          ALTER TABLE table.raw_data ADD IF NOT EXISTS
          PARTITION  (YEAR=${self:custom.year}, MONTH=${self:custom.month}, DAY=${self:custom.day}, hour=${self:custom.hour})
      ...