Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


AWS SAM Template

24 Sep 2022 »

makefile


pwd=$(shell pwd)
srcdir="$(pwd)/app"

define buildbash =
        rm -rf $(srcdir)/layer
        mkdir -p $(srcdir)/layer/python
        docker run -it -v $(srcdir)/layer/python:/local --rm python:3.7 \
                bash -c "
                pip     install -t /local requests pandas"
endef




build:
        $(buildbash)

package:
        aws cloudformation package \
                --s3-bucket sam-camproject \
                --template-file template.yaml \
                --output-template gen/template-generated.yaml

deploy:
        aws cloudformation deploy \
                --region sa-east-1 \
                --template-file gen/template-generated.yaml \
                --stack-name sam-cf-camproject

sam: package deploy

all: build package deploy

.ONESHELL:

template.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A starter AWS Lambda function.
Resources:
  app:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.7
      CodeUri: ./app/src
      Description: A starter AWS Lambda function.
      MemorySize: 2048
      Timeout: 30
      Role: arn:aws:iam::691262992979:role/lamda-s3-fullaccess
      Events:
        root:
          Type: Api
          Properties:
            Path: /
            Method: GET
        query:
          Type: Api
          Properties:
            Path: /query
            Method: POST
        search:
          Type: Api
          Properties:
            Path: /search
            Method: POST
      Layers:
        - !Ref appLayer

  appLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: simplejson-camera-image-panel
      ContentUri: ./app/layer
      CompatibleRuntimes:
        - python3.7
      LicenseInfo: 'MIT'

src/lambda_function.py

import json
import boto3
import requests


def lambda_handler(event, context):
    
    result = {}

    result = context

    response = {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "Content-Type",
            "Access-Control-Allow-Methods": "OPTIONS,GET"
        },
        body: JSON.stringify(result)
    };