Search code examples
aws-serverlessaws-step-functions

How to use the serverless environment variable in stepfunction parameter


I have a query with hardcoded dates used in the parameters section.Instead I want to pass them as environment variables.Any suggestions on how to parameterize the QueryString parameter?

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  
    


Solution

  • you can replace any value in your serverless.yml enclosed in ${} brackets,

    Serverless Framework Guide to Variables: https://www.serverless.com/framework/docs/providers/aws/guide/variables/

    for example, you can create a custom: section looking for environment variables, and if they are not present, you can have default values:

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