LoginSignup
11
1

More than 1 year has passed since last update.

目次

1.LocalStackとは
2.今回作成するもの
3.準備するもの
4.LocalStackの起動
5.S3バケットの作成
6.Lambda関数の作成
7.まとめ

1-LocalStackとは

・疑似的なAWS環境をローカルに構築することができる
・実際に料金はかからず、動作確認をすることが可能
 ⇒有料版もありますが、主要なサービスは無料版で使用することが出来ます
・導入が比較的楽

実際にAWS環境を使用しなくても動作確認ができることや、課金されないのはありがたいですね、、、。
筆者は色々起動しっぱなしにして月3000円×4か月くらい請求された経験があるため、非常にうれしいポイントです。

2-今回作成するもの

今回は簡単にS3とLambdaをそれぞれ作成していこうと思います

3-準備するもの

1.Docker環境

まずDockerとdocker-compoaseの動作する環境を用意する必要があります。
Macの方であればDocker Desktop for Mac、WindowsであればDocker Desktop for Windows をインストールします。

[Download Desktop Windows (stable)] のダウンロードボタンをクリックします。

2.docker-compose.yaml(AWSリソース等の定義ファイル)

次にdocker-compose.yamlを作成して、LocalStackで使用したいリソース等を定義していきます。

docker-compose.yaml
version: '3.7'
services:
  localstack:
    image: localstack/localstack
    ports:
      - "4566:4566"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=${DEBUG- }
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
      - LAMBDA_DOCKER_NETWORK=host
      - LAMBDA_EXECUTOR=docker-reuse
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"

4.LocalStackの起動

起動は以下のコマンドで行います。

$ docker-compose up -d

ブラウザからhttp://localhost:4566/healthでサービスが立ち上がっているかを確認
⇒runningになっていればOK!!

{
    "services": {
        "acm": "running",
        "apigateway": "running",
        "cloudformation": "running",
        "cloudwatch": "running",
        "dynamodb": "running",
        "dynamodbstreams": "running",
        "ec2": "running",
        "es": "running",
        "firehose": "running",
        "iam": "running",
        "sts": "running",
        "kinesis": "running",
        "kms": "running",
        "lambda": "running",
        "logs": "running",
        "redshift": "running",
        "route53": "running",
        "s3": "running",
        "secretsmanager": "running",
        "ses": "running",
        "sns": "running",
        "sqs": "running",
        "ssm": "running",
        "events": "running",
        "stepfunctions": "running",
        "swf": "running",
        "resourcegroupstaggingapi": "running",
        "resource-groups": "running",
        "support": "running"
    },
    "features": {
        "persistence": "disabled",
        "initScripts": "initialized"
    }
}

5-S3バケットの作成

test-bucketというS3バケットの作成、及びファイルのアップロードを行います。

$ aws s3 mb s3://test-bucket --endpoint-url=http://localhost:4566 --profile localstack make_bucket: test-bucket
  make_bucket: test-bucket
$ aws s3 ls s3://test-bucket/test-dir/ --endpoint-url=http://localhost:4566 --profile localstack
  2021-12-10 23:48:09         15 sample.txt

6-Lambda関数の作成

1.Lambda関数内の処理を記述

lambda.js
exports.myHandler = async (event, context) => {
    return `Hello Lambda`;
};

2.Lambda関数のデプロイ

上記のlambda.jsをzip化して以下のコマンドでLambda関数を作成します。
以下のようなログが出ればOK!!

$ aws --endpoint-url=http://localhost:4566 lambda create-function --function-name test  --runtime nodejs8.10 --handler lambda.myHandler --role r1  --zip-file fileb://dist.zip --region ap-northeast-1 --profile localstack
{   
    "FunctionName": "test",
    "FunctionArn": "arn:aws:lambda:ap-northeast:000000000000:function:test",
    "Runtime": "nodejs8.10",
    "Role": "r1",
    "Handler": "lambda.myHandler",
    "CodeSize": 148066,
    "Description": "",
    "Timeout": 3,
    "LastModified": "2021-12-19T14:02:37.548+0000",
    "CodeSha256": "Hpy+ZxGyZ59vvRiGq3abpYtXdfOUYjqwR9EfJmg2pjw=",        
    "Version": "$LATEST",
    "VpcConfig": {},
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "9ebad713-8d6e-4855-beb9-7a1c5037d265",
    "State": "Active",
    "LastUpdateStatus": "Successful"
}

3.実行

$ aws lambda --endpoint-url=http://localhost:4566 invoke --function-name test --profile localstack  result.log
{
    "StatusCode": 200,
    "LogResult": "",
    "ExecutedVersion": "$LATEST"
}
result.log
"Hello Lambda"

7-まとめ

今回は単純にリソースの作成をしてみました。

ここまで簡単にAWS環境をローカルに作成できるのはありがたいですね。CLIの練習や、実際のAWS環境にデプロイするのはちょっと面倒くさい、、。といった時にかなり役立つかなと思いました。
DBやAPIも組み合わせて一貫したテストや動作確認ができるので、普段からAWSを使用している方もぜひ参考にしてみて下さい。

11
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
1