CDK의 AWS Batch 환경 제작 준비 편
개요
CDK를 사용하여 AWS 환경을 만들 때의 방법을 요약하고 싶습니다.
이번에는 준비편이니 살살 하세요
(잘 돌아가지 않는 곳이 있어서...)
차리다
봐. AWS Summit CDK 피규어 준비해.
npm install -g aws-cdk
명령을 사용하여 cdk 설치 프로젝트 준비
cdk init sample-app --language typescript
라이브러리 설치
npm install @aws-cdk/aws-ecs
npm install @aws-cdk/aws-batch
npm install @aws-cdk/aws-ec2
npm install @aws-cdk/aws-iam
npm install @aws-cdk/aws-ecr
컴파일 준비
npm run watch
소스별 설명
new CdkStack(app, 'CdkStack');
. new CdkStack(app, 'CdkStack', {
env: {
region: 'ap-northeast-1',
account: 'XXXXXXXXX',
}
현재 생성된 CDK 정의
import * as cdk from '@aws-cdk/core';
import * as batch from '@aws-cdk/aws-batch';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as rds from '@aws-cdk/aws-rds';
import * as s3 from '@aws-cdk/aws-s3';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as ecs from '@aws-cdk/aws-ecs';
import * as ecr from '@aws-cdk/aws-ecr';
import { ContainerImage, EcrImage } from '@aws-cdk/aws-ecs';
import { Repository } from '@aws-cdk/aws-ecr';
export class CdkStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// 既存のVPCを取得
const vpc: ec2.IVpc = ec2.Vpc.fromLookup(this, 'VPC', {
vpcId: 'vpc-xxxxxxxxxxx',
});
// 既存のサブネットを取得
const selectSubnets: ec2.SelectedSubnets = vpc.selectSubnets({
subnets: [
ec2.Subnet.fromSubnetAttributes(this, 'Subnet', {
subnetId: 'subnet-xxxxxxxxxx',
availabilityZone: 'ap-northeast-1a',
routeTableId: 'rtb-xxxxxxxxxx',
}),
]
});
// 既存のセキュリティーグループを取得
const securityGroup: ec2.ISecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(
this, 'SecurityGroup', 'sg-xxxxxxxxx',
);
// バッチのロール
const batchRole: iam.IRole = new iam.Role(this, 'BatchRole', {
roleName: 'TestBatchRole',
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('batch.amazonaws.com'),
),
managedPolicies: [
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'AWSBatchServiceRole',
'arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole',
),
],
});
// Job用ロールを作成
const jobRole: iam.IRole = new iam.Role(this, 'JobRole', {
roleName: 'TestJobRole',
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal('ecs-tasks.amazonaws.com'),
),
managedPolicies: [
iam.ManagedPolicy.fromManagedPolicyArn(
this,
'AmazonECSTaskExecutionRolePolicy',
'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy',
),
],
});
// コンピューティング環境を作成 TODO ロールの設定ができていない
const computeEnvironment: batch.ComputeEnvironment = new batch.ComputeEnvironment(this, 'BatchCompute', {
computeEnvironmentName: 'TestComputeEnvironment',
computeResources: {
type: batch.ComputeResourceType.ON_DEMAND,
vpc: vpc,
vpcSubnets: selectSubnets,
securityGroups: [securityGroup],
},
serviceRole:batchRole
});
// 既存のリポジトリを指定
const repository = Repository.fromRepositoryName(this,'pandastestRepo','pandastest2')
const ecscontainerImage = ecs.ContainerImage.fromEcrRepository( repository,'latest')
// 引数と環境変数を適当に設定
const jobDefinec = new batch.JobDefinition(this, 'JobDefinition', {
jobDefinitionName: 'pandasTest',
container: {
command: ['1','3','A'],
environment: {'RDS_PASS': 'password','TEST':'ABC'},
image: ecscontainerImage,
jobRole: jobRole,
vcpus: 1,
memoryLimitMiB: 32,
},
});
}
}
Reference
이 문제에 관하여(CDK의 AWS Batch 환경 제작 준비 편), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/dmikita/items/40a5ce0df136a2e0e390텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)