Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template resource #26

Closed
nalbion opened this issue Nov 28, 2016 · 3 comments
Closed

template resource #26

nalbion opened this issue Nov 28, 2016 · 3 comments

Comments

@nalbion
Copy link

nalbion commented Nov 28, 2016

One of the downsides of the micro services architecture model is that there is more overhead because the configuration, management and deployment tasks are duplicated for each service. Our swagger schemas and Cloudformation templates are full of copy-and-pasted snippets.

In the example below, the commonality is extracted into LambdaTemplate, reducing the PutFunction to 3 lines and allowing the differences between the other functions to be more obvious.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  LambdaTemplate:
    Type: AWS::Serverless::TemplateFunction
    Properties:
      Handler: index.handler
      Runtime: nodejs4.3
      Policies: AWSLambdaDynamoDBExecutionRole
      CodeUri: s3://bucketName/codepackage.zip
      Environment:
        Variables:
          TABLE_NAME: !Ref Table

  PutFunction:
    Type: AWS::Serverless::Function
    Properties:
      # extra points if this can (optionally) be imported from an external file, even in a separate but shared repository
      Template: !Ref LambdaTemplate
  GetFunction:
    Type: AWS::Serverless::Function
    Properties:
      Template: !Ref LambdaTemplate
      Handler: index.getHandler
  DeleteFunction:
    Type: AWS::Serverless::Function
    Properties:
      Template: !Ref LambdaTemplate
      Events:
        Stream:
          Type: DynamoDB
          Properties:
            Stream: !GetAtt DynamoDBTable.StreamArn
            BatchSize: 100
            StartingPosition: TRIM_HORIZON

   DynamoDBTable:
     Type: AWS::DynamoDB::Table
     Properties: 
       AttributeDefinitions: 
         - AttributeName: id
           AttributeType: S
       KeySchema: 
         - AttributeName: id
           KeyType: HASH
       ProvisionedThroughput: 
         ReadCapacityUnits: 5
         WriteCapacityUnits: 5
       StreamSpecification:
         StreamViewType: streamview type
@dinvlad
Copy link

dinvlad commented Nov 28, 2016

That looks great, and would also solve the problem of 'default' parameters as in #24.
We'd only need to make sure that the properties from the 'template' would be overwritten if we also specified them locally.

@sanathkr
Copy link
Contributor

sanathkr commented Nov 2, 2017

I am starting to pick this task back up.

How do you imagine the local override to work for nested structures like Environment Variables. There are two options. Let me illustrate:

Assume this is the template (obvious properties omitted for simplicity):

FunctionTemplate:
  Type: AWS::Serverless::Template
  Properties:
    Environment:
      Variables:
         TableName: foo

MyFirstFunction:
  Properties:
    Template: !Ref FunctionTemplate
    Environment:
       Variables:
          DBName: bar

MySecondFunction:
  Properties:
    Template: !Ref FunctionTemplate

There are two options:

1. Environment variables get merged:: So, MyFirstFunction will get both TableName and DBName variable

2. Environment variables get overwritten: MyFirstFunction gets only DBName because you have overridden the Environment property entirely.

Option 1 is weird because it literally merges two different dictionaries which can cause complications. It will be even weird for Events section which can have a multi-level dictionary and even arrays.

Option 2 is easier to understand, easy to reason about and gives most of the benefits.

What do you guys prefer? What do you want to use in your templates?

@sanathkr
Copy link
Contributor

This is now available in the Globals section. Checkout: https://github.com/awslabs/serverless-application-model/releases/tag/1.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants