Amazon web services 如何配置AWS ECS Fargate任务/集群以使用Pulumi访问私有Docker容器注册表(如GitHub容器注册表)?
我正在使用Pulumi进行AWS Fargate群集设置,我当前的程序已经成功创建了一个群集,其中包括运行公共可访问容器映像的Fargate任务。我的映像基于Spring Boot(): 一切正常,但现在我需要使用私有容器映像Amazon web services 如何配置AWS ECS Fargate任务/集群以使用Pulumi访问私有Docker容器注册表(如GitHub容器注册表)?,amazon-web-services,spring-boot,docker,aws-fargate,pulumi,Amazon Web Services,Spring Boot,Docker,Aws Fargate,Pulumi,我正在使用Pulumi进行AWS Fargate群集设置,我当前的程序已经成功创建了一个群集,其中包括运行公共可访问容器映像的Fargate任务。我的映像基于Spring Boot(): 一切正常,但现在我需要使用私有容器映像ghcr.io/jonashackt/microserviceapi spring-boot-private:latest。切换代码以使用新映像,我的Fargate群集服务不断停止和启动新任务/容器。查看Fargate群集的服务“任务选项卡并切换到已停止任务状态,我看到大量
ghcr.io/jonashackt/microserviceapi spring-boot-private:latest
。切换代码以使用新映像,我的Fargate群集服务不断停止和启动新任务/容器。查看Fargate群集的服务“任务
选项卡并切换到已停止
任务状态,我看到大量的已停止(CannotPull…
错误如下:
// Define Container image published to the GitHub Container Registry
const service = new awsx.ecs.FargateService("microservice-api-spring-boot", {
taskDefinitionArgs: {
containers: {
blueprint_helloworld: {
image: "ghcr.io/jonashackt/microservice-api-spring-boot-private:latest",
memory: 768,
portMappings: [ albListener ],
// Access private GitHub Container Registry: we need to provide the Secret ARN as repositoryCredentials
// see https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/awsx/ecs/#Container-repositoryCredentials
repositoryCredentials: {
credentialsParameter: "arn:aws:secretsmanager:awsRegionHere:yourAccountIdHere:secret:githubContainerRegistryAccess-randomNumberHere",
}
},
},
executionRole: taskExecutionRole,
},
desiredCount: 2,
});
如果我点击其中一个停止的任务,我会看到以下停止原因
:
CannotPullContainerError: inspect image has been retried 1 time(s): failed to resolve ref "ghcr.io/jonashackt/microservice-api-spring-boot-private:latest": failed to authorize: failed to fetch anonymous token: unexpected status: 401 Unauthorized
因此,如何使用Pulumi配置对私有容器注册表(此处为GitHub容器注册表)的访问?在AWS文档中,有一个详细的指南。根据该指南,需要采取4个步骤:
0.获取令牌或凭据以访问专用容器注册表 如果您还没有访问令牌或凭据,则需要在我们的私有注册表中创建一个访问令牌或凭据,以便外部服务能够访问它。例如,使用GitHub容器注册表,我们至少需要使用
read:packages
作用域创建一个个人访问令牌()
1.创建包含令牌/凭证的AWS机密管理器机密到专用注册表 通过
存储新机密
按钮在上创建新机密。在GUI中选择其他类型的机密
和纯文本
-然后以JSON格式填写您的私人注册表凭据:
{
"username": "yourGitHubUserNameHere",
"password": "yourGitHubPATHere"
}
选择Next
并提供一个秘密名称,如githubContainerRegistryAccess
。再次点击Next并将禁用自动旋转作为默认设置。再次点击Store
创建秘密。最后复制秘密ARN,如ARN:aws:secretsmanager:awsregionher:yourcountId此处:Secret:githubContainerRegistryAccess randomNumberHere
到您的记事本或编辑器,以供以后参考
2.工艺任务执行角色(使用aws.iam.Role)包含私有容器注册表访问的inlinePolicy
我们需要将访问Secrets Manager Secret作为内联策略的权限添加到Fargate Tasks Execution角色。现在,新的awsx.ecs.FargateService
自动创建了这样一个任务执行角色,但我们以后无法访问它。实际上,我们需要自己创建整个任务。要创建aws.iam.Role
我们可以查看。Pulumiaws.iam.Role
由多个组件组成,我们需要三个组件:
assumerepolicy
:我自己并不想定义它,但是没有它就无法创建aws.iam.Role
。选择“sts:assumerepolice”
作为操作
和“ecs tasks.amazonaws.com”
作为服务主体至关重要
inlinePolicies
:此数组将采用任务访问Secrets Manager机密所需的InlinePolicy(也称为此角色创建的全部要点)。它是-真正关注资源中正确的arn
名称。其中一个正是我们的Secrets Manager Secret arn
managedPolicyArns
包含Pulumi附加到Fargate任务的默认策略(我只是查看了AWS控制台以找到他们的ARN)
以下是正确定义InlinePolicy所需的Pulumi代码:
const taskExecutionRole = new aws.iam.Role("microservice-api-spring-boot-execution", {
assumeRolePolicy: {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}, inlinePolicies: [
{
name: "ghcr-secret-access",
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: [
"kms:Decrypt",
"secretsmanager:GetSecretValue"
],
Resource: [
"arn:aws:secretsmanager:awsRegionHere:yourAccountIdHere:secret:githubContainerRegistryAccess-randomNumberHere",
"arn:aws:kms:awsRegionHere:yourAccountIdHere:key/key_id"
]
}]
})
},
],
managedPolicyArns: [
"arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
});
3.增强awsx.ecs.FargateService以使用我们的任务执行角色和存储凭据中的秘密ARN
现在,这是最后一步。我们需要使用创建的任务执行角色,并使用executionRole
参数将其附加到awsx.ecs.FargateService
。这看起来像这样:
// Define Container image published to the GitHub Container Registry
const service = new awsx.ecs.FargateService("microservice-api-spring-boot", {
taskDefinitionArgs: {
containers: {
blueprint_helloworld: {
image: "ghcr.io/jonashackt/microservice-api-spring-boot-private:latest",
memory: 768,
portMappings: [ albListener ],
// Access private GitHub Container Registry: we need to provide the Secret ARN as repositoryCredentials
// see https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/awsx/ecs/#Container-repositoryCredentials
repositoryCredentials: {
credentialsParameter: "arn:aws:secretsmanager:awsRegionHere:yourAccountIdHere:secret:githubContainerRegistryAccess-randomNumberHere",
}
},
},
executionRole: taskExecutionRole,
},
desiredCount: 2,
});
现在,一个pulumi up
应该按照预期启动Fargate任务,因为它们现在可以从私有GitHub容器注册表中提取容器映像。在AWS ECS群集视图中,您应该可以看到正在运行的任务: