Get TargetGroupArn from name?

痴心易碎 提交于 2020-12-12 11:12:03

问题


You use TargetGroupArn in a CF template for ECS services. I have a situation where the target group has already been created and I want to make this a param for the template

But those arn's are awful:

arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup/mytarget/4ed48ba353064a79

That unique number at the end makes this almost impossible. Can I reference the target by name instead of full arn in the template?

Maybe i can use Fn::GetAtt here but not sure what that looks like

This doesn't work:

  • TargetGroupArn: !GetAtt mytarget.TargetGroupName

I get error: An error occurred (ValidationError) when calling the CreateChangeSet operation: Template error: instance of Fn::GetAtt references undefined resource mytarget


回答1:


  1. If you want to use the available Target-group, You pass the target group name as the default parameter to the Service CF template.
  2. Internally refer the default parameter as the ref to the TargetGroupArn in the Action section of the LiestnerRule It will get the target group ARN.
  3. Check this link: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html
{
  "Parameters": {
      "VPC": {
      ...
      "TargetGroup": {
          "Description": "TargetGroup name for ListenerRule",
          "Type": "String",
          "Default": "my-target"
      }
  },
  "Resources": {
      "Service": {
      "TaskDefinition": {
      ....
      "ListenerRule": {
                  ....
              "Actions": [
                  {
                      "TargetGroupArn": {
                          "Ref": "TargetGroup"
                      },
                      "Type": "forward"
                  }
              ]
          }
      },
      "ServiceRole": {
  }
}



回答2:


Unfortunately with Target Groups, you won't be able to use convention to determine it's ARN due to the extra string at the end.

If the Target Group was created in Cloudformation, it's easy enough to get the ARN output by using !Ref myTargetGroup.

If the Target Group was created in another CF stack, try Exporting the Target Group ARN and use Fn::ImportValue when creating the ECS Service to input the Target Group ARN.

Type: "AWS::ECS::Service"
Properties: 
  ...
  LoadBalancers:
    - ContainerName: MyContainer
      ContainerPort: 1234
      TargetGroupArn: !ImportValue myExportedTargetGroupARN
  ...


来源:https://stackoverflow.com/questions/51228275/get-targetgrouparn-from-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!