Package: aws-cdk.aws-apprunner-alpha
AWS::AppRunner Construct Library---The APIs of higher level constructs in this module are experimental and under active development.
They are subject to non-backward compatible changes or removal in any future version. These are
not subject to theSemantic Versioningmodel and breaking changes will be
announced in the release notes. This means that while you may use them, you may need to update
your source code when upgrading to a newer version of this package.This module is part of theAWS Cloud Development Kitproject.importaws_cdk.aws_apprunner_alphaasapprunnerIntroductionAWS App Runner is a fully managed service that makes it easy for developers to quickly deploy containerized web applications and APIs, at scale and with no prior infrastructure experience required. Start with your source code or a container image. App Runner automatically builds and deploys the web application and load balances traffic with encryption. App Runner also scales up or down automatically to meet your traffic needs. With App Runner, rather than thinking about servers or scaling, you have more time to focus on your applications.ServiceTheServiceconstruct allows you to create AWS App Runner services withECR Public,ECRorGithubwith thesourceproperty in the following scenarios:Source.fromEcr()- To define the source repository fromECR.Source.fromEcrPublic()- To define the source repository fromECR Public.Source.fromGitHub()- To define the source repository from theGithub repository.Source.fromAsset()- To define the source from local asset directory.TheServiceconstruct implementsIGrantable.ECR PublicTo create aServicewith ECR Public:apprunner.Service(self,"Service",source=apprunner.Source.from_ecr_public(image_configuration=apprunner.ImageConfiguration(port=8000),image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest"))ECRTo create aServicefrom an existing ECR repository:importaws_cdk.aws_ecrasecrapprunner.Service(self,"Service",source=apprunner.Source.from_ecr(image_configuration=apprunner.ImageConfiguration(port=80),repository=ecr.Repository.from_repository_name(self,"NginxRepository","nginx"),tag_or_digest="latest"))To create aServicefrom local docker image asset directory built and pushed to Amazon ECR:You can specify whether to enable continuous integration from the source repository with theautoDeploymentsEnabledflag.importaws_cdk.aws_ecr_assetsasassetsimage_asset=assets.DockerImageAsset(self,"ImageAssets",directory=path.join(__dirname,"docker.assets"))apprunner.Service(self,"Service",source=apprunner.Source.from_asset(image_configuration=apprunner.ImageConfiguration(port=8000),asset=image_asset),auto_deployments_enabled=True)GitHubTo create aServicefrom the GitHub repository, you need to specify an existing App RunnerConnection.SeeManaging App Runner connectionsfor more details.apprunner.Service(self,"Service",source=apprunner.Source.from_git_hub(repository_url="https://github.com/aws-containers/hello-app-runner",branch="main",configuration_source=apprunner.ConfigurationSourceType.REPOSITORY,connection=apprunner.GitHubConnection.from_connection_arn("CONNECTION_ARN")))UsecodeConfigurationValuesto override configuration values with theAPIconfiguration source type.apprunner.Service(self,"Service",source=apprunner.Source.from_git_hub(repository_url="https://github.com/aws-containers/hello-app-runner",branch="main",configuration_source=apprunner.ConfigurationSourceType.API,code_configuration_values=apprunner.CodeConfigurationValues(runtime=apprunner.Runtime.PYTHON_3,port="8000",start_command="python app.py",build_command="yum install -y pycairo && pip install -r requirements.txt"),connection=apprunner.GitHubConnection.from_connection_arn("CONNECTION_ARN")))IAM RolesYou are allowed to defineinstanceRoleandaccessRolefor theService.instanceRole- The IAM role that provides permissions to your App Runner service. These are permissions that
your code needs when it calls any AWS APIs. If not defined, a new instance role will be generated
when required.To add IAM policy statements to this role, useaddToRolePolicy():importaws_cdk.aws_iamasiamservice=apprunner.Service(self,"Service",source=apprunner.Source.from_ecr_public(image_configuration=apprunner.ImageConfiguration(port=8000),image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest"))service.add_to_role_policy(iam.PolicyStatement(effect=iam.Effect.ALLOW,actions=["s3:GetObject"],resources=["*"]))accessRole- The IAM role that grants the App Runner service access to a source repository. It's required for
ECR image repositories (but not for ECR Public repositories). If not defined, a new access role will be generated
when required.SeeApp Runner IAM Rolesfor more details.VPC ConnectorTo associate an App Runner service with a custom VPC, definevpcConnectorfor the service.importaws_cdk.aws_ec2asec2vpc=ec2.Vpc(self,"Vpc",ip_addresses=ec2.IpAddresses.cidr("10.0.0.0/16"))vpc_connector=apprunner.VpcConnector(self,"VpcConnector",vpc=vpc,vpc_subnets=vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC),vpc_connector_name="MyVpcConnector")apprunner.Service(self,"Service",source=apprunner.Source.from_ecr_public(image_configuration=apprunner.ImageConfiguration(port=8000),image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest"),vpc_connector=vpc_connector)Secrets ManagerTo include environment variables integrated with AWS Secrets Manager, use theenvironmentSecretsattribute.
You can use theaddSecretmethod from the App RunnerServiceclass to include secrets from outside the
service definition.importaws_cdk.aws_secretsmanagerassecretsmanagerimportaws_cdk.aws_ssmasssm# stack: Stacksecret=secretsmanager.Secret(stack,"Secret")parameter=ssm.StringParameter.from_secure_string_parameter_attributes(stack,"Parameter",parameter_name="/name",version=1)service=apprunner.Service(stack,"Service",source=apprunner.Source.from_ecr_public(image_configuration=apprunner.ImageConfiguration(port=8000,environment_secrets={"SECRET":apprunner.Secret.from_secrets_manager(secret),"PARAMETER":apprunner.Secret.from_ssm_parameter(parameter),"SECRET_ID":apprunner.Secret.from_secrets_manager_version(secret,version_id="version-id"),"SECRET_STAGE":apprunner.Secret.from_secrets_manager_version(secret,version_stage="version-stage")}),image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest"))service.add_secret("LATER_SECRET",apprunner.Secret.from_secrets_manager(secret,"field"))HealthCheckTo configure the health check for the service, use thehealthCheckattribute.You can specify it by static methodsHealthCheck.httporHealthCheck.tcp.apprunner.Service(self,"Service",source=apprunner.Source.from_ecr_public(image_configuration=apprunner.ImageConfiguration(port=8000),image_identifier="public.ecr.aws/aws-containers/hello-app-runner:latest"),health_check=apprunner.HealthCheck.http(healthy_threshold=5,interval=Duration.seconds(10),path="/",timeout=Duration.seconds(10),unhealthy_threshold=10))