aws-samples / aws-sam-terraform-examples

MIT No Attribution
46 stars 14 forks source link

Ability to generate a "Lambda Application"? #3

Open lucknerjb opened 1 year ago

lucknerjb commented 1 year ago

Hello,

We're currently using Terraform and AWS SAM together and the lines are just blurry enough in terms of where certain things should live (like Cloudwatch alarms) that this sparked our interest as a way to continue to define all of our infrastructure in TF but continue to leverage AWS SAM for local development.

For extra context, the reason we're using TF and SAM together is that TF sets up the base infra (DB, Redis, etc...) and handles the Route53, SSM, Cloudfront, etc... infra for review apps / staging / production. Our devs started with SAM and we worked TF into the mix after.

Through my research and testing, there does not appear to be a way to create a Lambda application (set of functions grouped together) with Terraform and if I understand AWS SAM correctly, the application is what is run when we invoke sam local start-api.

Here is an excerpt of the SAM template generated by the example code in this repo:

AwsLambdaFunctionPublishBookReviewBFC97F47:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: publish-book-review
      Architectures:
      - x86_64
      Environment:
        Variables:
          DYNAMODB_TABLE_NAME: BookReviews
      Code: AwsLambdaFunctionPublishBookReviewBFC97F47
      Handler: index.lambda_handler
      PackageType: Zip
      Runtime: python3.8
      Layers: []
      Timeout: 30
    Metadata:
      BuildMethod: makefile
      ContextPath: ...
      ProjectRootDirectory: ...
      SamResourceId: aws_lambda_function.publish_book_review
      SkipBuild: false
      WorkingDirectory: ...

And here's an excerpt for a sample function we have in one of our SAM apps:

SampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: "src/api/sample"
      Handler: sample.lambda_handler
      Runtime: python3.9
      Tracing: Active
      Environment:
        Variables:
          REDIS_HOST: "redis-host"
          REDIS_KEY_PREFIX: "redis-key-prefix"
          DB_HOST: "database-host"
          DB_USERNAME: "database-username"
          DB_PASSWORD: "database-password"
          DB_NAME: "database-name"
          DB_PORT: "3306"
      Architectures:
        - x86_64
      Events:
        Sample:
          Type: Api
          Properties:
            Path: "/sample/{proxy+}"
            Method: "post"
      Layers:
        - !Ref SomeSharedLibs

Is there a way to achieve the same SAM template configuration? (Notice the missing Events section in the example-generated yaml.

Thanks!

lucknerjb commented 1 year ago

Ah snap... I just realized that we're generating a resource of type AWS::Serverless::Function whereas the example code is generating a resource of type AWS::Lambda::Function. Is there any way to bridge the gap here?