AWS Lambda(九)---用Lambda部署ECS

本次任務:

aws上創建容器repository,然後push一個image到這個repository,cloudtrail和cloudwatch檢測到變化後,調用lambda函數,執行部署到ECS的任務

1. 創建IAM用戶access key

(1)IAM頁面---user,創建一個user

(2)點擊創建好的user,創建access key

(3)本地安裝awscli客戶端,以下是linux安裝步驟:

pip install awscli

aws configure

然後輸入access key,即步驟(2)中生成的access key id 和secret access key。

然後用aws lambda list-functions 測試是否安裝成功

2. ECR和ECS頁面創建容器repository

(1)創建一個容器倉庫repository (左側導航欄可以看到,ECS和ECR在同一個頁面)

本地隨便建立一個docker項目,用docker build命令打包成image,例如docker build -t hello .

然後docker tag hello:latest xxx   (這個repository地址xxx可以是dockerhub的repository,也可以是aws裏ECS上面創建的repository地址。)

然後同一目錄下,用docker push xxx 上傳到遠程xxx這個repository上。

(2)創建ECS容器集羣cluster(包括service和cluster等)

還在ECS頁面,點擊cluster--get started

(3)選擇custom configure

 配置  memory限制128, 端口80

(4)task definition換一個你自己取的名字

接着配置taskdefinition,service,cluster等

3. 創建lambda函數

可以aws網站上創建,也可以用awscli客戶端使用命令aws create lambda-function

lambda函數內容如下:

import boto3
import os

## Setting variables and importing libraries.
region = "us-east-1"
client = boto3.client('ecs', region_name=region)

CONTAINER_NAME = os.environ['NAME']
DOCKER_IMAGE = os.environ['IMAGE']
# MEM_SIZE = os.environ['MEMORY']
FAMILY_DEF = os.environ['TASK_DEF']
CLUSTER_NAME = os.environ['CLUSTER']
SERVICE_NAME = os.environ['SERVICE']

def lambda_handler(event, context):
    print("----- STARTING -----")

    response = client.register_task_definition(
        family=FAMILY_DEF,
        #taskRoleArn='string',
        networkMode='awsvpc',
        containerDefinitions=[
            {
                'name': CONTAINER_NAME,
                ## Amazon URI for your Docker image in ECS
                'image': DOCKER_IMAGE,
                'memory': 300,
                'portMappings': [
                    {
                        'containerPort': 80,
                        'hostPort': 80,
                        'protocol': 'tcp'
                    },
                ],
                'essential': True,
            },
        ],
    )

    ## Setting our TaskDef for updating our service.
    response = client.update_service(
        cluster=CLUSTER_NAME,
        service=SERVICE_NAME,
        desiredCount=1,
        # taskDefinition=taskDefinitionRev,
        # networkConfiguration={
        #     'awsvpcConfiguration': {
        #         'subnets': [
        #             'subnet-0fc126592841487ea',
        #             'subnet-0ae7befc18f8498da'
        #         ],
        #         'securityGroups': [
        #             'sg-0a367400b36dae0bf'
        #         ],
        #         'assignPublicIp': 'ENABLED'
        #     }
        # },
        forceNewDeployment=True,
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 100
        }
    )
    print("Updated the service named {} under the cluster named {} with an updated task definition".format(SERVICE_NAME, CLUSTER_NAME))
    print("----- FINISHED -----")

4. 創建cloudtrail和cloudwatch的event

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章