[DOCKER] Learn AWS for free with LocalStack

Introduction

This article is part of a series of articles in Roadmap for former City Hall staff to change jobs to WEB programmers.

This article is written with the goal of providing a foothold for getting started with ** AWS **, a representative of cloud computing services.

Overseas, there is a story that although it has become cloud-based, "de-cloud-based" is progressing, returning to on-premises by saying, "After all, on-premises is safer." However, I think that cloud computing services, which allow you to easily procure computer resources and use ready-made services, are attractive technologies for server infrastructure engineers.

Therefore, it is expected that the wave of cloud computing will continue for a while, and it will be useful to learn AWS technology.

AWS

LocalStack

AWS has a free tier as a trial, but when you first come into contact with AWS, there are many features you don't understand and you may not be sure which ones are available for free.

In that respect, if you create a pseudo AWS environment locally with LocalStack and use it, you will not be charged no matter what you do, and if you make a mistake and destroy the entire virtual environment, nothing will happen from the beginning. You can start over.

Instead, you cannot use the WEB user interface (AWS console), but if you want to automate server construction etc., command line operation is essential anyway, so in that sense you are accustomed to AWS command line operation with LocalStack. I don't think it's bad to leave. (The AWS console may change the operation feeling a little ...)

Environment

Setup

# -- Python3.Building an environment using X--

#Update pip to the latest version
$ pip3 install --upgrade pip setuptools

#AWS CLI installation
$ pip3 install --upgrade aws

#Set up a profile for localstack in the AWS CLI
## AccessKeyID,SecretAccessKey can be any string
$ aws configure --profile=localstack
AWS Access Key ID [None]: dummy
AWS Secret Access Key [None]: dummy
Default region name [None]: ap-northeast-1
Default output format [None]: json

#localstack installation
$ pip3 install --upgrade localstack

#Version confirmation
$ localstack -v
0.12.1

Execution Start the LocalStack mock environment.

#Dockerfile by yourself, docker-compose.write yml and localstack/You may launch the localstack image as a container,
#You can easily start the mock environment using the localstack start command
## START_WEB environment variables:Whether to use the web user interface (default): 1)
##The web user interface is deprecated and should not be used
## EDGE_PORT environment variable:Specify API endpoint port for each AWS service (default): 4566)
## DEFAULT_REGION environment variable:Specify the default region for AWS services (default): us-east-1)
## ...For other environment variables https://github.com/localstack/See localstack
$ START_WEB=0 EDGE_PORT=5066 DEFAULT_REGION=ap-northeast-1 localstack start

# => API: tcp://localhost:5066
# => Region: ap-northeast-1

# =>Ctrl to exit+C key

Operation check

This time, I will create the following WEB system.

--Functions on the cloud: test-function --When executed, a file called {execution date and time} .txt is generated. --The contents of the file are This file generated at {execution date and time} . --Save the above file to storage on the cloud (test-bucket)

test.drawio.png

Aliase the AWS CLI command for LocalStack

When using the AWS CLI for LocalStack ʻaws --endpoint-url = http: // localhost: --profile = `is used in common. It will be.

Due to its long length, it is useful to register this command in an alias.

#AWS CLI command for LocalStack`laws`Register with the alias
$ alias laws='aws --endpoint-url=http://localhost:5066 --profile=localstack'

Creating an S3 bucket

--AWS S3 is a cloud storage service that can store file data on the cloud. --You can reserve a resource called ** bucket ** in S3 and store file data in it.

#Test to AWS S3 in a localstack environment using the AWS CLI-bucket Create a bucket
# $ aws s3 mb s3://<Bucket name> 
$ laws s3 mb s3://test-bucket
make_bucket: test-bucket

#Confirmation
$ laws s3 ls
2020-11-03 18:08:26 test-bucket

Creating a Lambda function

--AWS Lambda is a service that registers serverless programs (functions) on the cloud. --Lambda functions can execute arbitrary programs triggered by various events --The following languages are supported as program implementation languages. - Java, Go, PowerShell, Node.js, C#, Python, Ruby

Create lambda.py

This time, let's implement a Lambda function using Python3.

lambda.py


# boto3: AWS SDK for Python
import boto3
from boto3.session import Session
from datetime import datetime
import os

#Create a session to access AWS services
##AccessKeyID for localstack mock environment,SecretAccessKey can be dummy
session = Session(
    aws_access_key_id='dummy',
    aws_secret_access_key='dummy',
    region_name='ap-northeast-1'
)

#Connect to AWS S3
s3 = session.resource(
    service_name='s3',
    '''
    endpoint_url:
This script runs inside a Docker container, so
Localhost of Docker execution host machine:${EDGE_PORT}Need to look up
    LOCALSTACK_HOSTNAME environment variable:
Contains localhost IPv4 on the host machine running the LocalStack Docker container
    => http://${LOCALSTACK_HOSTNAME}:${EDGE_PORT}By specifying
        LocalStack(Docker)Communication between services in the environment is possible
    '''
    endpoint_url = 'http://' + os.environ['LOCALSTACK_HOSTNAME'] + ':' + os.environ['EDGE_PORT']
)

#Entry function
##In the following cases, lambda is used as a handler when registering with Lambda._Specify a handler
def lambda_handler(event, context):
    now = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') #Current date and time
    filename = now + '.txt' #file name: {Current date and time}.txt
    contents = f'This file generated at {now}.' #File contents
    bucket = 'test-bucket' #Save destination S3 bucket name

    # s3://test-bucket/{Current date and time}.Save file to txt
    s3.Bucket(bucket).put_object(Key=filename, Body=contents)
    
    #Return value after executing Lambda function
    return f'Saved: s3://{bucket}/{filename}'

Registered as a Lambda function

#Compress the created program into a zip
$ zip lambda.zip lambda.py

#Test to AWS Lambda using the AWS CLI-function.test zip-function Registered as a function
# $ aws lambda create-function
##    --function-name=<Function name>
##    --runtime=<Program language to use>
##    --role=<IAM role that can execute a function>* In the case of LocalStack, you can set an appropriate IAM role.
##    --handler=<Module name>.<Entry function>
##    --zip-file=fileb://<Zip compressed program file>
$ laws lambda create-function \
    --function-name='test-function' \
    --runtime='python3.7' \
    --role='arn:aws:iam::123456789012:role/service-role/test-exec-role' \
    --handler='lambda.lambda_handler' \
    --zip-file='fileb://./lambda.zip'

{
    "FunctionName": "test-function",
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:000000000000:function:test-function",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::123456789012:role/service-role/test-exec-role",
    "Handler": "lambda.lambda_handler",
    "CodeSize": 934,
    "Description": "",
    "Timeout": 3,
    "LastModified": "2020-11-03T11:26:52.329+0000",
    "CodeSha256": "MIcbKaalHpg1Ln2pWtxnooV+8uJif7yZNaZI1dM7lw0=",
    "Version": "$LATEST",
    "VpcConfig": {},
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "bad5c0cd-da0d-47f5-8b55-9e7b4b38a112",
    "State": "Active",
}

#If you want to delete the registered Lambda function
# $ aws lambda create-function --function-name=<Function name>

Lambda function execution

Try hitting the test-function function registered on the cloud (in this case, registered in the LocalStack virtual environment) by the above procedure.

#Run your Lambda function using the AWS CLI
# $ aws lambda invoke --function-name=<Function name> <Log file name>
$ laws lambda invoke --function-name='test-function' result.log
{
    "StatusCode": 200,
    "LogResult": "",
    "ExecutedVersion": "$LATEST"
}

#If there are no errors, the log file should contain the return value of your Lambda entry function.
$ cat result.log
"Saved: s3://test-bucket/2020-11-03-12-40-58.txt"

# S3 (test-bucket)Check if the file is saved in
$ laws s3 ls s3://test-bucket/
2020-11-03 21:40:58         43 2020-11-03-12-40-58.txt

#Check the contents of the above file
$ laws s3 cp s3://test-bucket/2020-11-03-12-40-58.txt -
This file generated at 2020-11-03-12-40-58.

The above is how to build an AWS mock environment using LocalStack and a simple WEB system.

That is all for this article.

Recommended Posts

Learn AWS for free with LocalStack
Learn Java with "So What" [For beginners]
Get S3 object size with AWS SDK for Ruby
Notify Slack of AWS bills daily with Lambda for Ruby
For beginners! Automatic deployment with Rails6 + CircleCI + Capistrano + AWS (EC2)
Preparation for developing with Rails
Build AWS Lambda with Quarkus
Try AWS Lambda Runtime Interface Emulator with Docker Desktop for M1