aws(学习笔记第三十一课) aws cdk深入学习(batch-arm64-instance-type)

aws(学习笔记第三十一课)

  • aws cdk深入学习

学习内容:

  • 深入练习aws cdk下部署batch-arm64-instance-type

1. 深入练习aws cdk下部署batch-arm64-instance-type

  1. 代码链接

  2. batch-arm64-instance-type代码测试

    • setup环境
      • setup virtual environment

        shell 复制代码
        python3 -m venv .venv
      • 激活python virtual environment

        shell 复制代码
        source .venv/Scripts/activate #这里使用的是git bash
      • 安装python的依赖包

        shell 复制代码
         pip install -r requirements.txt
      • 代码中cdk.jsonpython3进行替换
        这里的phthon3没有配置,所以不能执行python3,讲这里改成了python

  3. 代码执行

    • 预先执行cdk bootstrap
      cdk提供了bootstrap命令来创建cdk执行需要的前提条件。

      shell 复制代码
      CDKToolkit: creating CloudFormation changeset...
      CDKToolkit |  0/12 | 21:22:46 | REVIEW_IN_PROGRESS   | AWS::CloudFormation::Stack | CDKToolkit User Initiated
      CDKToolkit |  0/12 | 21:22:52 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | CDKToolkit User Initiated
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | FilePublishingRole
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | CloudFormationExecutionRole
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::ECR::Repository    | ContainerAssetsRepository
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::S3::Bucket         | StagingBucket
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | ImagePublishingRole
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | LookupRole
      CDKToolkit |  0/12 | 21:22:56 | CREATE_IN_PROGRESS   | AWS::SSM::Parameter     | CdkBootstrapVersion
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::SSM::Parameter     | CdkBootstrapVersion Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | FilePublishingRole Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::ECR::Repository    | ContainerAssetsRepository Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | CloudFormationExecutionRole Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | ImagePublishingRole Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::S3::Bucket         | StagingBucket Resource creation Initiated
      CDKToolkit |  0/12 | 21:22:57 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | LookupRole Resource creation Initiated
      CDKToolkit |  1/12 | 21:22:57 | CREATE_COMPLETE      | AWS::SSM::Parameter     | CdkBootstrapVersion
      CDKToolkit |  2/12 | 21:22:58 | CREATE_COMPLETE      | AWS::ECR::Repository    | ContainerAssetsRepository
      CDKToolkit |  3/12 | 21:23:12 | CREATE_COMPLETE      | AWS::S3::Bucket         | StagingBucket
      CDKToolkit |  3/12 | 21:23:14 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy   | StagingBucketPolicy
      CDKToolkit |  3/12 | 21:23:15 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy   | StagingBucketPolicy Resource creation Initiated
      CDKToolkit |  4/12 | 21:23:15 | CREATE_COMPLETE      | AWS::S3::BucketPolicy   | StagingBucketPolicy
      CDKToolkit |  5/12 | 21:23:16 | CREATE_COMPLETE      | AWS::IAM::Role          | ImagePublishingRole
      CDKToolkit |  6/12 | 21:23:16 | CREATE_COMPLETE      | AWS::IAM::Role          | FilePublishingRole
      CDKToolkit |  7/12 | 21:23:16 | CREATE_COMPLETE      | AWS::IAM::Role          | CloudFormationExecutionRole
      CDKToolkit |  7/12 | 21:23:16 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy
      CDKToolkit |  8/12 | 21:23:16 | CREATE_COMPLETE      | AWS::IAM::Role          | LookupRole
      CDKToolkit |  8/12 | 21:23:17 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy
      CDKToolkit |  8/12 | 21:23:17 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | DeploymentActionRole
      CDKToolkit |  8/12 | 21:23:18 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy Resource creation Initiated
      CDKToolkit |  8/12 | 21:23:18 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy Resource creation Initiated
      CDKToolkit |  8/12 | 21:23:19 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | DeploymentActionRole Resource creation Initiated
      CDKToolkit |  9/12 | 21:23:34 | CREATE_COMPLETE      | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy
      CDKToolkit | 10/12 | 21:23:34 | CREATE_COMPLETE      | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy
      CDKToolkit | 11/12 | 21:23:38 | CREATE_COMPLETE      | AWS::IAM::Role          | DeploymentActionRole
      CDKToolkit | 12/12 | 21:23:39 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | CDKToolkit
       ✅  Environment aws://081353481087/ap-northeast-1 bootstrapped.

      这里,cdk bootstrap命令执行完之后,会生成一个CDKToolkit名字的cloudformation

      如果由于平时的误删除之类的操作,会使CDKToolkit的创建的cdk deploy需要的前提条件遭到破坏。这时如果重新执行cdk bootstrap会导致没有差分执行出来。这时候需要将CDKToolkit这个cloudformation删除掉,之后执行cdk bootstrap命令,这样就会重新生成cdk deploy执行的所有条件。

    • 执行cdk --require-approval never deploy

      如果执行不加上--require-approval never,会导致错误,所以这里加上该选项。

    • Trouble Shouting

      如果存在默认的vpc,这里会出现如下错误。

      原因在于:
      Fn:GetAZs这个函数调用的话,会只返回default VPCAvailable Zone的数量,从而影响cdk的执行。

      对策:

      所以要删除default VPC,不过不用担心,default VPC可以重新创建,大可放心。

  4. 代码解析

    • 全体架构

    • 作成VPC

      python 复制代码
       # This resource alone will create a private/public subnet in each AZ as well as nat/internet gateway(s)
      vpc = ec2.Vpc(self, "VPC")

      创建一下service组件:

      • 一个VPC
      • 一个public subnet
      • 一个private subnet
      • 一个Internet Gateway
      • 一个NAT Gateway Compute Environment通过这个NAT Gateway访问internet
    • 创建Job Queue

      python 复制代码
      # Create AWS Batch Job Queue
      self.batch_queue = batch.JobQueue(self, "JobQueueArm64")
      ```![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/176bc23f45574046aa99e801861c9014.png)
    • 创建Compute Environment,并将每个Compute Environment分配给JobQueue

      python 复制代码
              # For loop to create Batch Compute Environments
              for i in range(count):
                  name = "MyBatchARM64Env" + str(i)
                  batch_environment = batch.ManagedEc2EcsComputeEnvironment(self, name,
                      spot=True,
                      spot_bid_percentage=75,
                      instance_types=[ec2.InstanceType("c7g.medium"),ec2.InstanceType("c7g.large")],
                      use_optimal_instance_classes=False,
                      vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT),
                      vpc=vpc
                  )
      
                  self.batch_queue.add_compute_environment(batch_environment, i)
    • 创建Job Defintion

      python 复制代码
             # Create ECS Job Definition to submit job in batch job queue.
              batch_jobDef = batch.EcsJobDefinition(self, "MyJobDefArm64",
                                                 container=batch.EcsEc2ContainerDefinition(self, "CDKJobDefArm64",
                                                     image=ecs.ContainerImage.from_registry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
                                                     command=["sleep", "60"],
                                                     memory=Size.mebibytes(512),
                                                     cpu=1
                                                 )
              )
  5. 执行job

    • 投入新的Job
    • Job执行成功
相关推荐
愚戏师12 分钟前
Python3 Socket 网络编程复习笔记
网络·笔记
降临-max1 小时前
JavaSE---网络编程
java·开发语言·网络·笔记·学习
大白的编程日记.2 小时前
【计算网络学习笔记】MySql的多版本控制MVCC和Read View
网络·笔记·学习·mysql
IMPYLH3 小时前
Lua 的 require 函数
java·开发语言·笔记·后端·junit·lua
车载测试工程师5 小时前
CAPL学习-IP API函数-2
网络·学习·tcp/ip·capl·canoe
YJlio6 小时前
进程和诊断工具学习笔记(8.29):ListDLLs——一眼看清进程里加载了哪些 DLL,谁在偷偷注入
android·笔记·学习
lkbhua莱克瓦247 小时前
集合进阶8——Stream流
java·开发语言·笔记·github·stream流·学习方法·集合
车载测试工程师7 小时前
CAPL学习-IP API函数-1
网络·学习·tcp/ip·capl·canoe·doip
爱学java的ptt7 小时前
jvm笔记
jvm·笔记
雾岛听蓝7 小时前
C++ 类和对象(一):从概念到实践,吃透类的核心基础
开发语言·c++·经验分享·笔记