AWS: AppRunner 构造库
本单元中更高层次的构件的API是实验性的并正在积极开发中. 在今后任何版本中,它们都须作非背向相容的修改或去除。 这些是 不适用 语义版本 模型和断开的更改 在发行笔记中宣布. 这意味着虽然你可能使用它们,但可能需要更新 当更新到此软件包的更新版本时,您的源代码.
这个模块是 AWS 云开发套件 项目.
import * as apprunner from '@aws-cdk/aws-apprunner-alpha';
导言
AWS App Runner是一种完全管理的服务,它使得开发者能够快速地部署集装箱化的网络应用程序和API,在规模上,不需要事先的基础设施经验. 从您的源代码或容器图像开始 。 App Runner自动构建并部署网络应用程序,并用加密方式加载流量平衡. App Runner 也自动缩放上下,以满足您的流量需要. 有了App Runner,而不是考虑服务器或缩放,你有更多的时间专注于你的应用程序.
服务
这个 Service 构造允许您使用 ECR Public, (中文(简体) ), ECR 或者说 Github 与 source 在下列情况下的属性:
Source.fromEcr()- 定义源寄存器ECR。 。 。 .Source.fromEcrPublic()- 定义源寄存器ECR Public。 。 。 .Source.fromGitHub()- 从Github repository。 。 。 .Source.fromAsset()- 从本地资产目录中定义来源.
这个 Service 构造工具 IGrantable。 。 。 .
ECR 公众
创建 Service eCR 公开 :
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
});
ECR( 电子格式)
创建 Service 从现有的ECR存储器中:
import * as ecr from 'aws-cdk-lib/aws-ecr';
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcr({
imageConfiguration: { port: 80 },
repository: ecr.Repository.fromRepositoryName(this, 'NginxRepository', 'nginx'),
tagOrDigest: 'latest',
}),
});
创建 Service 从本地 docker 图像资产目录创建并被推到 Amazon ECR:
您可以指定是否允许从源寄存器与 autoDeploymentsEnabled 旗帜.
import * as assets from 'aws-cdk-lib/aws-ecr-assets';
const imageAsset = new assets.DockerImageAsset(this, 'ImageAssets', {
directory: path.join(__dirname, 'docker.assets'),
});
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromAsset({
imageConfiguration: { port: 8000 },
asset: imageAsset,
}),
autoDeploymentsEnabled: true,
});
GitHub 图像
创建 Service 从 GitHub 寄存器中, 您需要指定一个已有的 App 运行器 Connection。 。 。 .
见 管理 App 运行器连接 更多细节.
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromGitHub({
repositoryUrl: 'https://github.com/aws-containers/hello-app-runner',
branch: 'main',
configurationSource: apprunner.ConfigurationSourceType.REPOSITORY,
connection: apprunner.GitHubConnection.fromConnectionArn('CONNECTION_ARN'),
}),
});
使用 codeConfigurationValues 以 API 配置源类型 .
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromGitHub({
repositoryUrl: 'https://github.com/aws-containers/hello-app-runner',
branch: 'main',
configurationSource: apprunner.ConfigurationSourceType.API,
codeConfigurationValues: {
runtime: apprunner.Runtime.PYTHON_3,
port: '8000',
startCommand: 'python app.py',
buildCommand: 'yum install -y pycairo && pip install -r requirements.txt',
},
connection: apprunner.GitHubConnection.fromConnectionArn('CONNECTION_ARN'),
}),
});
IAM 角色
您可以定义 instanceRole 财务报告和审定财务报表 accessRole 联 合 国 Service。 。 。 .
instanceRole - 为您的 App Runner 服务提供权限的 IAM 角色 。 这些是权限
当代码调用 AWS API 时, 您需要代码 。 如果没有定义, 将生成一个新的实例角色
必要时.
将IAM政策声明添加到这一角色中,使用 addToRolePolicy()数字 :
import * as iam from 'aws-cdk-lib/aws-iam';
const service = new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
});
service.addToRolePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject'],
resources: ['*'],
}))
accessRole - 赋予App Runner服务访问源存储器的IAM角色。 这是需要的
ECR图像存储器(但不用于ECR公共存储器). 如果没有定义,将产生一个新的访问角色
必要时.
见 App 运行器 IAM 角色 更多细节.
自动缩放配置
将 App Runner 服务与自定义自动缩放配置关联起来, 定义 autoScalingConfiguration 为服务.
const autoScalingConfiguration = new apprunner.AutoScalingConfiguration(this, 'AutoScalingConfiguration', {
autoScalingConfigurationName: 'MyAutoScalingConfiguration',
maxConcurrency: 150,
maxSize: 20,
minSize: 5,
});
new apprunner.Service(this, 'DemoService', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
autoScalingConfiguration,
});
VPC 连接器
要将 App Runner 服务与自定义的 VPC 连接,请定义 vpcConnector 为服务.
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const vpc = new ec2.Vpc(this, 'Vpc', {
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16')
});
const vpcConnector = new apprunner.VpcConnector(this, 'VpcConnector', {
vpc,
vpcSubnets: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }),
vpcConnectorName: 'MyVpcConnector',
});
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
vpcConnector,
});
VPC 入侵连接
让您的 App Runner 服务私有化, 并且只能从 VPC 内部访问 isPubliclyAccessible 财产并与之关联 VpcIngressConnection 资源.
设立 VpcIngressConnection,指定一个 VPC,一个 VPC 界面端点,以及 App Runner 服务.
还有,你必须准备好 isPubliclyAccessible 属性在 r Service 改为 false。 。 。 .
更多信息见 允许进入的私人端口。 。 。 .
import * as ec2 from 'aws-cdk-lib/aws-ec2';
declare const vpc: ec2.Vpc;
const interfaceVpcEndpoint = new ec2.InterfaceVpcEndpoint(this, 'MyVpcEndpoint', {
vpc,
service: ec2.InterfaceVpcEndpointAwsService.APP_RUNNER_REQUESTS,
privateDnsEnabled: false,
});
const service = new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: {
port: 8000,
},
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
isPubliclyAccessible: false, // set false
});
new apprunner.VpcIngressConnection(this, 'VpcIngressConnection', {
vpc,
interfaceVpcEndpoint,
service,
});
双堆叠
要使用双堆栈( IPv4 和 IPv6) 来配置您的 公共网络, 设置 ipAddressType 改为 IpAddressType.DUAL_STACK。 。 。 .
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
ipAddressType: apprunner.IpAddressType.DUAL_STACK,
});
说明:目前,App Runner只支持公共端点的双堆栈. 私人端点只支持IPv4. 如果你更新了一个使用双层公共端点到私人端点的服务, 您的 App Runner 服务将默认仅支持私人端点的 IPv4 并失败 以接收来自IPv6端点的流量.
机密管理器
要纳入与 AWS 保密管理器整合的环境变量,请使用 environmentSecrets 属性.
你可以用这个 addSecret 来自 App 运行器的方法 Service 分类包括来自外部的秘密
服务定义.
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
import * as ssm from 'aws-cdk-lib/aws-ssm';
declare const stack: Stack;
const secret = new secretsmanager.Secret(stack, 'Secret');
const parameter = ssm.StringParameter.fromSecureStringParameterAttributes(stack, 'Parameter', {
parameterName: '/name',
version: 1,
});
const service = new apprunner.Service(stack, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: {
port: 8000,
environmentSecrets: {
SECRET: apprunner.Secret.fromSecretsManager(secret),
PARAMETER: apprunner.Secret.fromSsmParameter(parameter),
SECRET_ID: apprunner.Secret.fromSecretsManagerVersion(secret, { versionId: 'version-id' }),
SECRET_STAGE: apprunner.Secret.fromSecretsManagerVersion(secret, { versionStage: 'version-stage' }),
},
},
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
})
});
service.addSecret('LATER_SECRET', apprunner.Secret.fromSecretsManager(secret, 'field'));
使用客户管理的密钥
要使用客户管理的密钥进行源加密,请使用 kmsKey 属性.
import * as kms from 'aws-cdk-lib/aws-kms';
declare const kmsKey: kms.IKey;
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
kmsKey,
});
健康检查
要配置服务的健康检查,请使用 healthCheck 属性.
您可以用静态方法指定 HealthCheck.http 或者说 HealthCheck.tcp。 。 。 .
new apprunner.Service(this, 'Service', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
healthCheck: apprunner.HealthCheck.http({
healthyThreshold: 5,
interval: Duration.seconds(10),
path: '/',
timeout: Duration.seconds(10),
unhealthyThreshold: 10,
}),
});
可观察性配置
要将 App Runner 服务与自定义可观察配置关联起来,请使用 observabilityConfiguration 属性.
const observabilityConfiguration = new apprunner.ObservabilityConfiguration(this, 'ObservabilityConfiguration', {
observabilityConfigurationName: 'MyObservabilityConfiguration',
traceConfigurationVendor: apprunner.TraceConfigurationVendor.AWSXRAY,
});
new apprunner.Service(this, 'DemoService', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
observabilityConfiguration,
});