Skip to content

Commit

Permalink
change deploy style from zip to raw.
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-wada-yusuke committed Jun 13, 2018
1 parent 6a3da43 commit aa8c381
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 242 deletions.
48 changes: 29 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ SHELL = /usr/bin/env bash -xeuo pipefail

include Makevars

TEST_INTEG_TARGETS := $(shell find test -name '*.bats')

GIT_COMMIT := $(shell git log -n 1 --format=%h)
ZIP_FILE := $(GIT_COMMIT).zip
UPLOAD_FILE := deploy/$(ZIP_FILE)
BASE := src/functions
DIR := $(sort $(dir $(wildcard $(BASE)/*/)))
TARGETS := $(patsubst $(BASE)/%/, %, $(DIR))
BASE := src/functions/handlers
TEMPLATES := $(sort $(notdir $(wildcard templates/template_*.yaml)))
TARGETS := $(patsubst template_%.yaml, %, $(TEMPLATES))

UPLOAD_TASK := $(addprefix upload-, $(TARGETS))
DEPLOY_TASK := $(addprefix deploy-, $(TARGETS))


all: guard-env clean test-unit test-integ dist $(UPLOAD_TASK) $(DEPLOY_TASK)

all: guard-env clean format test-unit dist $(UPLOAD_TASK) $(DEPLOY_TASK)
@echo $(UPLOAD_TASK)
@echo $(DEPLOY_TASK)

display:
@echo $(TARGETS)
@echo $(UPLOAD_TASK)
@echo $(DEPLOY_TASK)

Expand All @@ -27,19 +32,19 @@ localstack-up:
localstack-stop:
@docker-compose stop localstack

test-unit:
python -m pytest
format:
-yapf -ir src/
-yapf -ir test/

test-integ: localstack-up
@echo $(TEST_INTEG_TARGETS)
@echo $(DOCKER_NAME)
@DOCKER_NAME=$(DOCKER_NAME) bats $(TEST_INTEG_TARGETS)
test-unit:
@python -m pytest test/

clean:
-rm -rf deploy/
-rm packaged-*.yaml
-rm template_*.yaml

dist:
dist: format
@pip install -r requirements.txt -t deploy
@cp -R src deploy
@find deploy -type f -name \*.pyc -o -name \*.pyo | xargs rm
Expand All @@ -57,7 +62,7 @@ deploy: guard-env $(DEPLOY_TASK)
@echo $(UPLOAD_TASK)
@echo $(DEPLOY_TASK)

upload-%: $(UPLOAD_FILE)
upload-%: guard-env $(UPLOAD_FILE)
@ if [ "${*}" = "" ]; then \
echo "Target is not set"; \
exit 1; \
Expand All @@ -67,28 +72,33 @@ upload-%: $(UPLOAD_FILE)
else \
s3_keyname="${*}/$(ZIP_FILE)" && \
echo $${s3_keyname} && \
aws s3 cp $(UPLOAD_FILE) $(S3_BUCKET_URL)/$${s3_keyname} ; \
aws s3 cp $(UPLOAD_FILE) s3://$${env}-$(S3_BUCKET)/$${s3_keyname} ; \
fi

deploy-%: template_%.yaml guard-env
deploy-%: templates/template_%.yaml guard-env guard-account_id
@ if [ "${*}" = "" ]; then \
echo "Target is not set"; \
exit 1; \
elif [ ! -d "$(BASE)/${*}" ]; then \
echo "Target directory $(BASE)/$* does not exists."; \
exit 1; \
else \
cat templates/template_${*}.yaml templates/lambda_common_parameters.yaml > template_${*}.yaml && \
stack_name_hyphen=$(subst _,-,$(*)) && \
s3_keyname="${*}/$(ZIP_FILE)" && \
aws cloudformation package \
--template-file template_${*}.yaml \
--s3-bucket $(S3_BUCKET_URL) \
--s3-bucket $${env}-$(S3_BUCKET) \
--output-template-file packaged-${*}.yaml && \
aws cloudformation deploy \
--template-file packaged-${*}.yaml \
--stack-name $${env}-${*}-lambda \
--stack-name $${env}-$${stack_name_hyphen}-lambda \
--capabilities CAPABILITY_IAM \
--no-fail-on-empty-changeset \
--parameter-overrides \
Env=$${env} \
AccountId=$${account_id} \
Region=ap-northeast-1 \
CodeKey=$${s3_keyname} ; \
fi

Expand All @@ -103,7 +113,7 @@ guard-%:
localstack-up \
localstack-stop \
test-unit \
test-integ \
dist \
upload \
clean \
format
2 changes: 1 addition & 1 deletion Makevars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# enter your own environment values.

S3_BUCKET_URL := s3://hero-lambda-deploy
S3_BUCKET := hero-lambda-deploy
DOCKER_NAME := pythonlambdatemplate_default
18 changes: 0 additions & 18 deletions environments/sam-local.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from builtins import Exception
import os
from src.functions.heroes.utils import *
from src.functions.handlers.heroes.utils import *

DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
HERO_TABLE_NAME = os.getenv('HERO_TABLE_NAME')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
def epoc_by_second_precision(time: datetime):
return decimal.Decimal(time.replace(microsecond=0).timestamp())


class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
return super(DecimalEncoder, self).default(o)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from builtins import Exception
import os
from src.functions.heroes.utils import *
from src.functions.handlers.heroes.utils import *

ENV = os.getenv('ENV')
DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
Expand Down
62 changes: 62 additions & 0 deletions src/functions/handlers/sponsors/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""This is python3.6 program."""

import boto3
import datetime
import uuid
from builtins import Exception
import os
from src.functions.handlers.heroes.utils import *

ENV = os.getenv('ENV')
DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
SPONSOR_TABLE_NAME = os.getenv('SPONSOR_TABLE_NAME')

print(DYNAMODB_ENDPOINT)
print(SPONSOR_TABLE_NAME)

DYNAMO = boto3.resource(
'dynamodb',
endpoint_url=DYNAMODB_ENDPOINT
)

DYNAMODB_TABLE = DYNAMO.Table(SPONSOR_TABLE_NAME)



def get(event, context):
try:
sponsor_id = event['id']

dynamo_response = DYNAMODB_TABLE.get_item(
Key={
'id': sponsor_id
}
)

response = json.dumps(dynamo_response['Item'], cls=DecimalEncoder, ensure_ascii=False)

return response

except Exception as error:
raise error

def put(event, context):
try:
sponsor_id = str(uuid.uuid4())

name = event.get('name')
address = event.get('address')
updated_at = epoc_by_second_precision(datetime.now())

response = DYNAMODB_TABLE.put_item(
Item={
'id': sponsor_id,
'name': name,
'address': address,
'updated_at': updated_at,
'created_at': updated_at,
}
)
return response
except Exception as error:
raise error
43 changes: 43 additions & 0 deletions templates/lambda_common_parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Parameters:
Env:
Type: String
Default: local
DynamoDBEndpoint:
Type: String
Default: https://dynamodb.ap-northeast-1.amazonaws.com/
HeroTableName:
Type: String
Default: CM-Heroes
OfficeTableName:
Type: String
Default: CM-Offices
SponsorTableName:
Type: String
Default: CM-Sponsors
AccountId:
Type: String
Default: 99999999999
BucketName:
Type: String
Default: hero-lambda-deploy
CodeKey:
Type: String
Default: heroes/0000.zip
GetHeroesFunctionName:
Type: String
Default: heroes-get_heroes
PutHeroesFunctionName:
Type: String
Default: heroes-put_heroes
GetOfficesFunctionName:
Type: String
Default: heroes-get_offices
PutOfficesFunctionName:
Type: String
Default: heroes-put_offices
GetSponsorsFunctionName:
Type: String
Default: heroes-get_sponsores
PutSponsorsFunctionName:
Type: String
Default: heroes-put_sponsores
36 changes: 9 additions & 27 deletions template_heroes.yaml → templates/template_heroes.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple CRUD webservice. State is stored in a SimpleTable (DynamoDB) resource.
Parameters:
Env:
Type: String
Default: local
DynamoDBEndpoint:
Type: String
Default: https://dynamodb.ap-northeast-1.amazonaws.com/
HeroTableName:
Type: String
Default: CM-Heroes
BucketName:
Type: String
Default: hero-lambda-deploy
CodeKey:
Type: String
Default: heroes/0000.zip
Resources:
GetHeroes:
Type: AWS::Serverless::Function
Properties:
FunctionName:
Fn::Sub: ${Env}-heroes-GetHeroes
Handler: src/functions/heroes/index.get
Fn::Sub: ${Env}-${GetHeroesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/handlers/heroes/index.get
Runtime: python3.6
CodeUri:
Bucket: !Ref BucketName
Key: !Ref CodeKey
Policies: AmazonDynamoDBReadOnlyAccess
CodeUri: src/functions
Environment:
Variables:
ENV: !Ref Env
Expand All @@ -39,13 +22,12 @@ Resources:
Type: AWS::Serverless::Function
Properties:
FunctionName:
Fn::Sub: ${Env}-heroes-PutHeroes
Fn::Sub: ${Env}-${PutHeroesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/heroes/index.put
Runtime: python3.6
CodeUri:
Bucket: !Ref BucketName
Key: !Ref CodeKey
Policies: AmazonDynamoDBFullAccess
CodeUri: src/functions
Environment:
Variables:
ENV: !Ref Env
Expand Down
Loading

0 comments on commit aa8c381

Please sign in to comment.