diff --git a/scripts/samlocal.sh b/scripts/samlocal.sh index 244b110..03689f0 100755 --- a/scripts/samlocal.sh +++ b/scripts/samlocal.sh @@ -1,7 +1,9 @@ #!/bin/bash set -e +CWD=$(dirname "${0}") DOCKER_NETWORK_NAME=lambda-local +DDB_TABLE_NAME=testUserTable DDB_CONTAINER_PREFIX=dynamodblocalpythonrapid DDB_CONTAINER_NAME=${DDB_CONTAINER_PREFIX}$(date "+%Y%m%d%H%M%S") DDB_LOCAL_PORT=8001 @@ -22,15 +24,17 @@ if [ ! "x${EXISTING_CONTAINERS}" = "x" ]; then docker stop ${EXISTING_CONTAINERS} docker rm ${EXISTING_CONTAINERS} fi -docker run -d --name ${DDB_CONTAINER_NAME} --net ${DOCKER_NETWORK_NAME} -p ${DDB_LOCAL_PORT}:${DDB_LOCAL_PORT} amazon/dynamodb-local +docker run -d --name ${DDB_CONTAINER_NAME} --net ${DOCKER_NETWORK_NAME} -p ${DDB_LOCAL_PORT}:8000 amazon/dynamodb-local # Create DDB schema /usr/local/bin/aws dynamodb create-table --endpoint-url http://localhost:${DDB_LOCAL_PORT} \ - --table-name testUserTable \ + --table-name ${DDB_TABLE_NAME} \ --attribute-definitions AttributeName=user_id,AttributeType=S \ --key-schema AttributeName=user_id,KeyType=HASH \ --billing-mode PAY_PER_REQUEST # Launch SAM Local -echo "{\"Parameters\": {\"DYNAMODB_ENDPOINT_URL\": \"http://${DDB_CONTAINER_NAME}:${DDB_LOCAL_PORT}\"}}" | jq . > ${PATH_TO_ENVVAR} -sam local start-api --docker-network ${DOCKER_NETWORK_NAME} --env-vars ${PATH_TO_ENVVAR} +echo "{\"Parameters\": {\"DYNAMODB_ENDPOINT_URL\": \"http://${DDB_CONTAINER_NAME}:${DDB_LOCAL_PORT}\", \"DYNAMODB_TABLE_NAME\": \"${DDB_TABLE_NAME}\"}}" | jq . > ${PATH_TO_ENVVAR} +cd ${CWD}/../provisioning +cdk synth +sam local start-api --template-file cdk.out/ApiStack.template.json --docker-network ${DOCKER_NETWORK_NAME} --env-vars ${PATH_TO_ENVVAR} diff --git a/template.yaml b/template.yaml deleted file mode 100644 index 50cc6b7..0000000 --- a/template.yaml +++ /dev/null @@ -1,203 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Outputs: - APIHandlerArn: - Value: - Fn::GetAtt: - - APIHandler - - Arn - APIHandlerName: - Value: - Ref: APIHandler - EndpointURL: - Value: - Fn::Sub: https://${RestAPI}.execute-api.${AWS::Region}.amazonaws.com/api/ - RestAPIId: - Value: - Ref: RestAPI - -Globals: - Function: - Environment: - Variables: - DYNAMODB_ENDPOINT_URL: null - DYNAMODB_TABLE_NAME: testUserTable - -Resources: - APIHandler: - Properties: - CodeUri: ./app - Handler: app.dispatch_request - MemorySize: 128 - Role: - Fn::GetAtt: - - DefaultRole - - Arn - Runtime: python3.8 - Timeout: 60 - Type: AWS::Serverless::Function - APIHandlerInvokePermission: - Properties: - Action: lambda:InvokeFunction - FunctionName: - Ref: APIHandler - Principal: apigateway.amazonaws.com - SourceArn: - Fn::Sub: - - arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestAPIId}/* - - RestAPIId: - Ref: RestAPI - Type: AWS::Lambda::Permission - DefaultRole: - Properties: - AssumeRolePolicyDocument: - Statement: - - Action: sts:AssumeRole - Effect: Allow - Principal: - Service: lambda.amazonaws.com - Sid: '' - Version: '2012-10-17' - Policies: - - PolicyDocument: - Statement: - - Action: - - logs:CreateLogGroup - - logs:CreateLogStream - - logs:PutLogEvents - Effect: Allow - Resource: arn:aws:logs:*:*:* - Version: '2012-10-17' - PolicyName: DefaultRolePolicy - Type: AWS::IAM::Role - RestAPI: - Properties: - DefinitionBody: - definitions: - Empty: - title: Empty Schema - type: object - info: - title: hoge - version: '1.0' - paths: - /user: - put: - consumes: - - application/json - produces: - - application/json - responses: - '200': - description: 200 response - schema: - $ref: '#/definitions/Empty' - x-amazon-apigateway-integration: - contentHandling: CONVERT_TO_TEXT - httpMethod: POST - passthroughBehavior: when_no_match - responses: - default: - statusCode: '200' - type: aws_proxy - uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandler.Arn}/invocations - /user/{user_id}: - delete: - consumes: - - application/json - parameters: - - in: path - name: user_id - required: true - type: string - produces: - - application/json - responses: - '200': - description: 200 response - schema: - $ref: '#/definitions/Empty' - x-amazon-apigateway-integration: - contentHandling: CONVERT_TO_TEXT - httpMethod: POST - passthroughBehavior: when_no_match - responses: - default: - statusCode: '200' - type: aws_proxy - uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandler.Arn}/invocations - get: - consumes: - - application/json - parameters: - - in: path - name: user_id - required: true - type: string - produces: - - application/json - responses: - '200': - description: 200 response - schema: - $ref: '#/definitions/Empty' - x-amazon-apigateway-integration: - contentHandling: CONVERT_TO_TEXT - httpMethod: POST - passthroughBehavior: when_no_match - responses: - default: - statusCode: '200' - type: aws_proxy - uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandler.Arn}/invocations - patch: - consumes: - - application/json - parameters: - - in: path - name: user_id - required: true - type: string - produces: - - application/json - responses: - '200': - description: 200 response - schema: - $ref: '#/definitions/Empty' - x-amazon-apigateway-integration: - contentHandling: CONVERT_TO_TEXT - httpMethod: POST - passthroughBehavior: when_no_match - responses: - default: - statusCode: '200' - type: aws_proxy - uri: - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandler.Arn}/invocations - schemes: - - https - swagger: '2.0' - x-amazon-apigateway-binary-media-types: - - application/octet-stream - - application/x-tar - - application/zip - - audio/basic - - audio/ogg - - audio/mp4 - - audio/mpeg - - audio/wav - - audio/webm - - image/png - - image/jpg - - image/jpeg - - image/gif - - video/ogg - - video/mpeg - - video/webm - EndpointConfiguration: EDGE - StageName: api - Type: AWS::Serverless::Api -Transform: AWS::Serverless-2016-10-31