sapessi / serverless-sam

Serverless framework plugin to export AWS SAM templates for a service
Apache License 2.0
152 stars 26 forks source link

environment variables are not copied to template.json #12

Closed theburningmonk closed 6 years ago

sapessi commented 6 years ago

Can you provide a sample serverless template? I've add an integration test using the env sample and I see environment variables exported correctly

theburningmonk commented 6 years ago
service: big-mouth

plugins:
  - serverless-pseudo-parameters
  - serverless-sam

provider:
  name: aws
  runtime: nodejs6.10
  iamRoleStatements:
    - Effect: Allow
      Action: dynamodb:scan
      Resource: arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/restaurants
    - Effect: Allow
      Action: execute-api:Invoke
      Resource: arn:aws:execute-api:#{AWS::Region}:#{AWS::AccountId}:*/*/GET/restaurants

functions:
  get-index:
    handler: functions/get-index.handler
    events:
      - http:
          path: /
          method: get
    environment:
      restaurants_api: xxx
      cognito_user_pool_id: xxx
      cognito_client_id: xxx

  get-restaurants:
    handler: functions/get-restaurants.handler
    events:
      - http:
          path: /restaurants/
          method: get
          authorizer: aws_iam
    environment:
      restaurants_table: restaurants

  search-restaurants:
    handler: functions/search-restaurants.handler
    events:
      - http:
          path: /restaurants/search
          method: post
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:374852340823:userpool/xxx
    environment:
      restaurants_table: restaurants

resources:
  Resources:
    restaurantsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: restaurants
        AttributeDefinitions:
          - AttributeName: name
            AttributeType: S
        KeySchema:
          - AttributeName: name
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
theburningmonk commented 6 years ago

I'm using sls v1.22.0 and just tested this again on serverless-sam v0.0.2 and the problem is still there

sapessi commented 6 years ago

Maybe it's the version of the framework. I just ran the test with the latest one. Let me switch to the same version.

sapessi commented 6 years ago

I just ran this through my local version of serverless-sam and I get this template out:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'SAM template for Serverless framework service: '
Resources:
  restaurantsTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: restaurants
      AttributeDefinitions:
        - AttributeName: name
          AttributeType: S
      KeySchema:
        - AttributeName: name
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
  GetIndex:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: functions/get-index.handler
      Runtime: nodejs6.10
      CodeUri: /private/tmp/test_repos/test/.serverless/big-mouth.zip
      MemorySize: 128
      Timeout: 3
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action: 'dynamodb:scan'
              Resource: >-
                arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/restaurants
            - Effect: Allow
              Action: 'execute-api:Invoke'
              Resource: >-
                arn:aws:execute-api:#{AWS::Region}:#{AWS::AccountId}:*/*/GET/restaurants
      Environment:
        Variables:
          restaurants_api: xxx
          cognito_user_pool_id: xxx
          cognito_client_id: xxx
      Events:
        Event1:
          Type: Api
          Properties:
            Path: /
            Method: get
            RestApiId:
              Ref: BigMouth
  BigMouth:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: dev
      DefinitionBody:
        swagger: '2.0'
        info:
          title:
            Ref: 'AWS::StackName'
        paths:
          /:
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri:
                  'Fn::Sub': >-
                    arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetIndex.Arn}/invocations
              responses: {}
          /restaurants/:
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri:
                  'Fn::Sub': >-
                    arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetRestaurants.Arn}/invocations
              responses: {}
              security:
                - Aws_iam: []
          /restaurants/search:
            post:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri:
                  'Fn::Sub': >-
                    arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SearchRestaurants.Arn}/invocations
              responses: {}
        securityDefinitions:
          Aws_iam:
            type: apiKey
            name: Aws_iam
            in: header
            x-amazon-apigateway-authtype: oauth2
            x-amazon-apigateway-authorizer:
              type: token
              authorizerUri:
                'Fn::Sub': >-
                  arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Aws_iam.Arn}/invocations
              identityValidationExpression: '^x-[a-z]+'
              authorizerResultTtlInSeconds: 60
  GetIndexLambdaPermission:
    Type: 'AWS::Lambda::Permission'
    DependsOn:
      - GetIndex
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName:
        Ref: GetIndex
      Principal: apigateway.amazonaws.com
  GetRestaurants:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: functions/get-restaurants.handler
      Runtime: nodejs6.10
      CodeUri: /private/tmp/test_repos/test/.serverless/big-mouth.zip
      MemorySize: 128
      Timeout: 3
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action: 'dynamodb:scan'
              Resource: >-
                arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/restaurants
            - Effect: Allow
              Action: 'execute-api:Invoke'
              Resource: >-
                arn:aws:execute-api:#{AWS::Region}:#{AWS::AccountId}:*/*/GET/restaurants
      Environment:
        Variables:
          restaurants_table: restaurants
      Events:
        Event1:
          Type: Api
          Properties:
            Path: /restaurants/
            Method: get
            RestApiId:
              Ref: BigMouth
  Aws_iamResourcePolicy:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName:
        'Fn::GetAtt':
          - Aws_iam
          - Arn
      Principal: apigateway.amazonaws.com
      SourceAccount:
        Ref: 'AWS::AccountId'
  GetRestaurantsLambdaPermission:
    Type: 'AWS::Lambda::Permission'
    DependsOn:
      - GetRestaurants
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName:
        Ref: GetRestaurants
      Principal: apigateway.amazonaws.com
  SearchRestaurants:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: functions/search-restaurants.handler
      Runtime: nodejs6.10
      CodeUri: /private/tmp/test_repos/test/.serverless/big-mouth.zip
      MemorySize: 128
      Timeout: 3
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action: 'dynamodb:scan'
              Resource: >-
                arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/restaurants
            - Effect: Allow
              Action: 'execute-api:Invoke'
              Resource: >-
                arn:aws:execute-api:#{AWS::Region}:#{AWS::AccountId}:*/*/GET/restaurants
      Environment:
        Variables:
          restaurants_table: restaurants
      Events:
        Event1:
          Type: Api
          Properties:
            Path: /restaurants/search
            Method: post
            RestApiId:
              Ref: BigMouth
  SearchRestaurantsLambdaPermission:
    Type: 'AWS::Lambda::Permission'
    DependsOn:
      - SearchRestaurants
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName:
        Ref: SearchRestaurants
      Principal: apigateway.amazonaws.com

See answer in #13 about running the repo version of the plugin - I'd rather test with you to make sure it works as expected before I push a 0.0.3 to npm

sapessi commented 6 years ago

Hey @theburningmonk , to make life easier, I've just published a 0.0.3 of the plugin to npm, you should be able to install from there (or upgrade)

theburningmonk commented 6 years ago

I just cloned the repo, put it into the node_modules in the project folder, and ran sls sam export -o template.yml and the template.yml now has both environment variables as well as the relatively path, let me try the publish 0.0.3 version as well

theburningmonk commented 6 years ago

good, 0.0.3 works as well 👍