pulumi / pulumi-awsx

AWS infrastructure best practices in component form!
https://www.pulumi.com/docs/guides/crosswalk/aws/
Apache License 2.0
212 stars 106 forks source link

Run the ECS example, see error of https://logs.undefined.amazonaws.com/ preventing service up #1279

Open zhangcheng opened 2 months ago

zhangcheng commented 2 months ago

What happened?

I was trying the ECS example.

The container service won't start properly. I saw error as:

Task stopped at: 2024-05-06T11:20:53.694Z
ResourceInitializationError: failed to validate logger args: create stream has been retried 7 times: failed to create Cloudwatch log stream: RequestError: send request failed caused by: Post "https://logs.undefined.amazonaws.com/": dial tcp: lookup logs.undefined.amazonaws.com on 172.31.0.2:53: no such host : exit status 1

Clearly this part of https://logs.undefined.amazonaws.com/ isn't correct, since my region config is us-west-2 and all resources were created there.

Tried to explicitly specify region as AI suggested, didn't work.

aws_provider = aws.Provider("aws_provider", region="us-west-2")

Was using the latest pulumi-awsx==2.9.0 and pulumi-aws==6.33.1, downgraded to much earlier version, didn't work.

Unless I explicitly set log_group, as:

log_group = aws.cloudwatch.LogGroup("my-log-group")

# and this arg
log_configuration=awsx.ecs.TaskDefinitionLogConfigurationArgs(
    log_driver="awslogs",
    options={
        "awslogs-group": log_group.name,
        "awslogs-region": "us-west-2",  # Replace with your region
        "awslogs-stream-prefix": "ecs",
    },
),

This solved the issue finally.

Example

import pulumi
import pulumi_aws as aws
import pulumi_awsx as awsx

lb = awsx.lb.ApplicationLoadBalancer("lb")
cluster = aws.ecs.Cluster("cluster")

service = awsx.ecs.FargateService("service",
    cluster=cluster.arn,
    assign_public_ip=True,
    desired_count=2,
    task_definition_args=awsx.ecs.FargateServiceTaskDefinitionArgs(
        container=awsx.ecs.TaskDefinitionContainerDefinitionArgs(
            name="my-service",
            image="nginx:latest",
            cpu=128,
            memory=512,
            essential=True,
            port_mappings=[awsx.ecs.TaskDefinitionPortMappingArgs(
                container_port=80,
                target_group=lb.default_target_group,
            )],
        ),
    ))

pulumi.export("url", pulumi.Output.concat("http://", lb.load_balancer.dns_name))

Output of pulumi about

CLI
Version      3.115.1
Go Version   go1.22.2
Go Compiler  gc

Plugins
KIND      NAME    VERSION
language  python  unknown

Host
OS       darwin
Version  14.4.1
Arch     arm64

This project is written in python: executable='/Users/czhang/.asdf/shims/python3' version='3.11.8'

Current Stack: teng01/ecs/dev

TYPE                             URN
pulumi:pulumi:Stack              urn:pulumi:dev::ecs::pulumi:pulumi:Stack::ecs-dev
pulumi:providers:awsx            urn:pulumi:dev::ecs::pulumi:providers:awsx::default_2_7_0
pulumi:providers:aws             urn:pulumi:dev::ecs::pulumi:providers:aws::aws_provider
awsx:lb:ApplicationLoadBalancer  urn:pulumi:dev::ecs::awsx:lb:ApplicationLoadBalancer::lb
pulumi:providers:aws             urn:pulumi:dev::ecs::pulumi:providers:aws::default_6_0_4
aws:ecs/cluster:Cluster          urn:pulumi:dev::ecs::aws:ecs/cluster:Cluster::cluster

Found no pending operations associated with dev

Backend
Name           pulumi.com
URL            https://app.pulumi.com/teng01
User           teng01
Organizations  teng01
Token type     personal

Dependencies:
NAME        VERSION
pip         24.0
setuptools  65.5.0

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

t0yv0 commented 2 months ago

Thanks so much for posting this! To clarify, in your final working example you are passing log_configuration to awsx.ecs.FargateServiceTaskDefinitionArgs to make this work ?

It appears that https://github.com/pulumi/pulumi-awsx/issues/1112 also brought up the logs.undefined.amazonaws.com error message so we have an opportunity to improve the provider by picking a better working default logging configuration so that the example works out of the box.

zhangcheng commented 2 months ago

Thanks so much for posting this! To clarify, in your final working example you are passing log_configuration to awsx.ecs.FargateServiceTaskDefinitionArgs to make this work ?

My final working example is:

task_definition_args=awsx.ecs.FargateServiceTaskDefinitionArgs(
    container=awsx.ecs.TaskDefinitionContainerDefinitionArgs(
        name="my-service",
        image="nginx:latest",
        cpu=128,
        memory=512,
        essential=True,
        log_configuration=awsx.ecs.TaskDefinitionLogConfigurationArgs(
            log_driver="awslogs",
            options={
                "awslogs-group": log_group.name,
                "awslogs-region": "us-west-2",  # Replace with your region
                "awslogs-stream-prefix": "ecs",
            },
        ),
    ),
)