How to get the output from a Stack after creating it?

0

I have a simple stack with VPC, Cluster, RDS, and Fargate Services. I'm having trouble getting the "Service URL" from a fargate service to use as a parameter in another fargate service. The cloudformation creates output for it, but I can't find a way to get it to export it as a not random name of CfnOutput.

Example: Outputs:

TestABC.TestABCLoadBalancerDNSEACC5E7A = XYZ.region.elb.amazonaws.com

TestABC.TestABCServiceURL2A142FC1 = http://XYZ.region.elb.amazonaws.com

Stack ARN: arn:aws:cloudformation:region:AWSID:stack/TestABC/ArnNumber

✨ Total time: 120s

I'm using the class ApplicationLoadBalancedFargateService for the fargate services and the language Java for CDK.

1 Antwort
1
Akzeptierte Antwort

Here is an example for Java SDK

Modify your stack to include a CfnOutput for the Load Balancer's DNS name:

package com.myorg;

import software.amazon.awscdk.core.*;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ecs.Cluster;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedFargateService;
import software.amazon.awscdk.services.ecs.ContainerImage;

public class FargateStack extends Stack {
    public FargateStack(final Construct scope, final String id, final StackProps props) {
        super(scope, id, props);

        Vpc vpc = Vpc.Builder.create(this, "MyVpc").maxAzs(3).build();
        Cluster cluster = Cluster.Builder.create(this, "MyCluster").vpc(vpc).build();

        ApplicationLoadBalancedFargateService fargateService = ApplicationLoadBalancedFargateService.Builder.create(this, "MyFargateService")
                .cluster(cluster)
                .cpu(256)
                .desiredCount(1)
                .taskImageOptions(
                        ApplicationLoadBalancedTaskImageOptions.builder()
                                .image(ContainerImage.fromRegistry("amazon/amazon-ecs-sample"))
                                .build())
                .memoryLimitMiB(512)
                .publicLoadBalancer(true)
                .build();

        // Export the Load Balancer DNS name
        CfnOutput.Builder.create(this, "ServiceUrl")
                .exportName("MyFargateServiceUrl") // This is the key name you will use to import in other stacks
                .value(fargateService.getLoadBalancer().getLoadBalancerDnsName())
                .build();
    }
}

public class MyApp extends App {
    public static void main(final String[] args) {
        App app = new App();
        new FargateStack(app, "MyFargateStack", new StackProps.builder().build());
        app.synth();
    }
}

Use the Exported URL in Another Service or Stack

import software.amazon.awscdk.core.Fn;

public class AnotherServiceStack extends Stack {
    public AnotherServiceStack(final Construct scope, final String id, final StackProps props) {
        super(scope, id, props);

        // Import the URL
        String importedUrl = Fn.importValue("MyFargateServiceUrl");

        // Use the imported URL here for whatever needs it
        System.out.println("Imported Service URL: " + importedUrl);
    }
}
profile picture
EXPERTE
beantwortet vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen