我正在尝试使用 AutoScalingGroup 从我的 CodeDeploy 引用现有 AutoScalingGroup。from_auto_scaling_group_name 静态方法,以便与 CodePipeline 集成以实现 EC2/本地部署的自动化。我有以下代码片段供您参考。
# Refer existing AutoScaling Group
asg_1 = autoscaling.AutoScalingGroup.from_auto_scaling_group_name(self, "AutoScaleGroup", "WSAutoscaleStack-webServerAsgIdASG12345-XXXXXX")
# EC2 Deployment Groups
deployment_group = codedeploy.ServerDeploymentGroup(self, "CodeDeployDeploymentGroup", deployment_group_name="MyDeploymentGroup", install_agent=True, auto_scaling_groups=[asg_1])
在使用 'cdk ls' 验证堆栈后,我收到一条错误消息:
jsii.errors.JSIIError: Cannot get policy fragment of AMIPipelineStack/AutoScaleGroup, resource imported without a role
据我所知,引用的资源应该作为一个对象导入,这样我就可以使用它的所有依赖项,包括资源中的 iam.role。有什么想法吗?
答案 0 :(得分:1)
看起来 fromAutoScalingGroupName 方法没有“导入”角色 (see here)
您拥有的一种选择是自己实施该导入。上面链接的导入类看起来像(在 Typescript 中):
public static fromAutoScalingGroupNameWithRole(scope: Construct, id: string, autoScalingGroupName: string, roleArn:string): IAutoScalingGroup {
class ImportWithRole extends AutoScalingGroupBase {
public autoScalingGroupName = autoScalingGroupName;
public autoScalingGroupArn = Stack.of(this).formatArn({
service: 'autoscaling',
resource: 'autoScalingGroup:*:autoScalingGroupName',
resourceName: this.autoScalingGroupName,
});
public readonly osType = ec2.OperatingSystemType.UNKNOWN;
public readonly grantPrincipal = iam.Role.fromRoleArn(this, `${id}-role`, roleArn)
}
return new ImportWithRole(scope, id);
}
您可以执行的另一个操作(如果适用于您的用例)是真正将自动缩放组及其角色导入 Cloudformation 堆栈。然后将使用 CDK/Cloudformation 堆栈管理资源,您可以使用标准 AutoScalingGroup 构造函数并提供您的角色。这里的缺点是目前这是一个相当痛苦的过程(参见link)