2
2

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 1 year has passed since last update.

AWS日記26 (AWS Fargate)

Last updated at Posted at 2021-04-30

はじめに

今回は AWS Fargate を試します。
スケジュールでタスクを実行する機能を作ります。

[今回作成した SAMテンプレート]
(https://github.com/tanaka-takurou/serverless-fargate-go)

準備

AWS SAM の準備をします

AWS Fargateの資料
Fargate を使用した Amazon ECS の開始方法

Dockerの資料
Docker ドキュメント

作業手順の概略

  • Dockerfile作成
  • 管理コンソールのAmazon ECR リポジトリからイメージリポジトリの作成
  • 「プッシュコマンドの表示」の手順に従い Docker Image を作成、プッシュ
  • AWS SAM アプリケーションをデプロイ

Dockerfile作成

FROM golang:alpine
COPY main.go /go/

AWS SAM テンプレート作成

AWS SAM テンプレートで ECS , CloudWatch の設定をします。

[参考資料]
AWS SAM テンプレートを作成する

AWS ECSの設定は以下の部分

  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Ref ProjectName

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: !Ref ProjectName
      RequiresCompatibilities:
        - FARGATE
      Cpu: !Ref TaskCpu
      Memory: !Ref TaskMemory
      NetworkMode: awsvpc
      ExecutionRoleArn: !GetAtt EcsTaskExecutionRole.Arn
      TaskRoleArn: !GetAtt EcsTaskRole.Arn
      ContainerDefinitions:
        - Name: app
          Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${ProjectName}
          EntryPoint:
            - 'sh'
            - '-c'
          Environment:
            - Name: TZ
              Value: Asia/Tokyo
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-region: !Ref 'AWS::Region'
              awslogs-group: !Ref LogGroup
              awslogs-stream-prefix: app
          Essential: true

終わりに

AWS Fargate を使用し、スケジュールでタスクを実行する機能を作りました。
ECS Exec を利用したコンテナへのアクセスも行える為、今後試そうと思います。

参考資料
2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?