2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS ECS Fargate を使ってS3にファイルをアップロードする

Last updated at Posted at 2021-01-31

コード

image.png

upload_s3.py
import boto3
import sys

credentials = sys.argv
up_file = "test_upload.txt"
bucket = "test-bucket-upload-s3"
object_name = "test_upload.txt"

session = boto3.Session(aws_access_key_id=credentials[1],
                        aws_secret_access_key=credentials[2],
                        aws_session_token=credentials[3])
s3_client = session.client('s3')
s3_client.upload_file(up_file, bucket, object_name)
execute.sh
apt update
apt-get install jq -y

AWS_CONTAINER_CREDENTIAL=`curl -s http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`
export AWS_ACCESS_KEY_ID=`echo "$AWS_CONTAINER_CREDENTIAL" | jq .AccessKeyId -r`
export AWS_SECRET_ACCESS_KEY=`echo "$AWS_CONTAINER_CREDENTIAL" | jq .SecretAccessKey -r`
export AWS_SESSION_TOKEN=`echo "$AWS_CONTAINER_CREDENTIAL" | jq .Token -r`

cd /mnt

python3 upload_s3.py $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY $AWS_SESSION_TOKEN
FROM python:latest

RUN pip3 install boto3

ADD . /mnt

CMD ["sh", "/mnt/execute.sh"]

コード説明

  • upload_s3.py

公式ドキュメントをコピペすればいける。

The AWS SDK for Python provides a pair of methods to upload a file to an S3 bucket.
The upload_file method accepts a file name, a bucket name, and an object name. The method handles large files by splitting them into smaller chunks and uploading each chunk in parallel.

  • execute.sh

公式ドキュメントのコマンドをほぼコピペすればいける。

コンテナ内から、以下のコマンドを使用して認証情報をクエリできます。

curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

出力:

{
    "AccessKeyId": "ACCESS_KEY_ID",
    "Expiration": "EXPIRATION_DATE",
    "RoleArn": "TASK_ROLE_ARN",
    "SecretAccessKey": "SECRET_ACCESS_KEY",
    "Token": "SECURITY_TOKEN_STRING"
}

上のjson出力から AccessKeyId , SecretAccessKey , Token を取得する。

  • Dockerfile

特になし

ECR プッシュ

EC2インスタンスにIAMロールを割り当ててECRにログインする。

IAMロールを割り当てよう。
起動中でもアタッチできるが時間がかかるので一旦インスタンスを停止する。(インスタンス起動設定のときにアタッチするのが無難)

image.png

お試しなのでECRFullAccessをつける。
抵抗があれば許可するアクションの詳細は公式ドキュメントにあるので見てほしい。
https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/security_iam_service-with-iam.html

image.png

ログインコマンドは覚える必要はない。以下の「プッシュコマンドの表示」を押せばよい。

image.png

コマンドの横のコピーボタンを押して張り付ける作業を繰り返す。

image.png

S3アクセス許可設定

「編集する」をクリック

image.png

「パブリックアクセスをすべてブロック」のチェックを外す

これで自分のバケットが世界公開となる。

image.png

ECS Fargate

タスク定義のとき、ロールの指定が必要

タスクロールのほうにs3にファイルをアップロードできるポリシーをアタッチしたロールを設定する。
なお、タスク実行ロールは自動で作成されるので気にしなくてよい。

image.png

後は実行!!

参考記事

boto3ライブラリ コード例
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

AWS ECR IAM ロール
https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/task-iam-roles.html

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?