Day 5 of Python Advent Calendar 2016 This is the article on the 5th day of Fusic Advent Calendar 2016.
Nice to meet you, my name is seike460. I usually touch PHP and the server.
Suddenly I want to be serverless in fashion! I thought, The current situation is that PHP cannot be serverless on AWS, which I use in a friendly manner. (To be precise, you can do it if you do your best, but I will leave that story aside.)
I can't help what I can't do, so I thought about serverless in other languages, I decided to go serverless using Python, which I've been attracted to recently.
If you try to configure Lambda and API Gateway without using anything, ~~ It's too annoying and I feel like I'm going crazy ~~ I got the impression that it was inefficient and couldn't stand the actual operation.
Therefore, I selected and implemented the official Amazon Python library "chalice".
If you have an AWS account, you can do serverless Hello World in less than 10 minutes just by running the Quickstart section. ~~ After all, the name that stimulates the heart of the kitchen is the best, the Holy Grail! The Holy Grail! ~~ Since the installation method has already been written in the README, the installation method will be omitted.
This time, the goal was to send the Json to API Gateway and save the Json in S3. It is assumed that an API that receives data will be installed and that data will be used via S3.
OS:macOS v10.12(Sierra) Python:2.7.10
First, prepare an IAM role for chalice. This time, we will set it regardless of the authority. For the time being, I will give authority to Aya. ** * Actually, you should think about it! Self-responsibility !! **
↓ It looks like this.
Then set the credentials in your development environment.
~/.aws/config
[default]
aws_access_key_id=I can't show you! !! !! ]
aws_secret_access_key=I can't show you! !! !! ]
region=ap-northeast-1 ← Tokyo Region
Hello world is ready to go.
I will also touch AWS, so I will also include boto3. If you want to use virtualenvs at this time, install it in the environment that uses chalice.
boto3
(chalice) $ pip install boto3
--Reference - boto3 - virtualenvs
Let's hello world Create a save2S3 project.
new-project
(chalice) $ chalice new-project save2S3
Then, the following directory structure will be created.
save2S3
save2S3
├── .chalice
│ └── config.json
├── .gitignore
├── app.py
└── requirements.txt
Edit this app.py to make it serverless. Let's open app.py
app.py
from chalice import Chalice
app = Chalice(app_name='save2S3')
@app.route('/')
def index():
return {'hello': 'world'}
# The view function above will return {"hello": "world"}
# whenver you make an HTTP GET request to '/'.
#
# Here are a few more examples:
#
# @app.route('/hello/{name}')
# def hello_name(name):
# # '/hello/james' -> {"hello": "james"}
# return {'hello': name}
#
# @app.route('/users', methods=['POST'])
# def create_user():
# # This is the JSON body the user sent in their POST request.
# user_as_json = app.json_body
# # Suppose we had some 'db' object that we used to
# # read/write from our database.
# # user_id = db.create_user(user_as_json)
# return {'user_id': user_id}
#
# See the README documentation for more examples.
#
The atmosphere that seems to be able to do hello world is amazing. It seems that chalice has hello world, so let's deploy it on AWS immediately.
deploy
(chalice) $ chalice deploy
Initial creation of lambda function.
Creating role
Creating deployment package.
Lambda deploy done.
Initiating first time deployment...
Deploying to: dev
https://[Your unique Path].execute-api.ap-northeast-1.amazonaws.com/dev/
It was very easy to deploy. By default, it is created in a Path called / dev. If you want to create it in a Path called api, you can use the following command.
deploy_to_api
(chalice) $ chalice deploy api
I will try to access the URL immediately.
deploy
(chalice) $ curl https://[Your unique Path].execute-api.ap-northeast-1.amazonaws.com/dev/
{"hello": "world"}
This completes your chalice debut. Show off to your neighbor, "I can use the Holy Grail." ** It's not my fault if the result is disappointing. Self-responsibility**
The top is good! I write a little code like that.
app.py
from chalice import Chalice
import boto3
import json
# for S3
clientS3 = boto3.client('s3')
yourBucketPath = '[Write your bucket here! ]'
app = Chalice(app_name='save2S3')
@app.route('/save', methods=['POST'], content_types=['application/json'])
def save():
saveJson = app.current_request.json_body
if (validateKey(saveJson) == False):
return {'error':'please input key'}
SavePath = saveJson['key'] + '.json'
clientS3.put_object(Bucket=yourBucketPath, Key=SavePath, Body=json.dumps(saveJson, ensure_ascii=False))
return {'save':saveJson['key']}
def validateKey(saveJson):
if (saveJson.has_key("key") == False):
return False
return True
Only the minimum Key Validate will be saved.
I also write a little code to check this guy. ** * I wrote this in python3. ** (lambda supports python3)
postJson.py
import urllib.request
import json
url = "https://[Your unique Path].execute-api.ap-northeast-1.amazonaws.com/dev/save"
method = "POST"
headers = {"Content-Type" : "application/json"}
obj = {"key" : "seike460", "val" : "I can use the Holy Grail"}
json_data = json.dumps(obj).encode("utf-8")
request = urllib.request.Request(
url,
data=json_data,
method=method,
headers=headers
)
with urllib.request.urlopen(request) as response:
response_body = response.read().decode("utf-8")
print(response_body)
When you run this, it's a ** * python3 command !! **
deploy
$ python3 postJson.py
{"save": "seike460"}
It should be well saved in S3 as well. At this point, you are a server wrestler. Let's be serverless to your heart's content. We can do serverless at this stage. It seems that our serverless sergeant will be overwhelmed theoretically, so I will stop.
This time PHPer tried using Python for the purpose of being serverless. inside that,
I felt that, this time it was a story that was close to AWS, I will continue to write something with Python material.
I want to touch Django
Recommended Posts