Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-ecs): Assign public IP is not supported for this launch type #13348

Closed
mostafafarzaneh opened this issue Mar 2, 2021 · 5 comments
Closed
Assignees
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container guidance Question that needs advice or information.

Comments

@mostafafarzaneh
Copy link

mostafafarzaneh commented Mar 2, 2021

I am trying to create an ECS service using the EC2 launch type and want to assign a public IP address to it.

Reproduction Steps

task_definition = ecs.Ec2TaskDefinition(self, "TD",
                network_mode=ecs.NetworkMode.AWS_VPC,
                proxy_configuration=appmesh_config,
                task_role=iam.Role.from_role_arn(self, "TaskRole",
                                                 self.ecs_task_role.role_arn),
                )

service = ecs.CfnService(self, "SRV",
        capacity_provider_strategy=[ecs.CfnService.CapacityProviderStrategyItemProperty(
                base=0,capacity_provider=self.capacity_provider.ref,weight=1)],
        cluster=self.cluster.cluster_arn,
        desired_count=1,
        network_configuration=ecs.CfnService.NetworkConfigurationProperty(
                awsvpc_configuration=ecs.CfnService.AwsVpcConfigurationProperty(
                        subnets=self.network.vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC).subnet_ids,
                        assign_public_ip="ENABLED",
                        security_groups=[self.ecs_host_security_group.security_group_id])),
        service_registries=[ecs.CfnService.ServiceRegistryProperty(
                    registry_arn=cloud_map.service_arn)], 
        task_definition=task_definition.task_definition_arn
        )

What did you expect to happen?

I want to assign public IP address to the EC2

What actually happened?

I get the Assign public IP is not supported for this launch type error

Environment

  • CDK CLI Version : 1.91.0

Other

I should mention that because I use a capacity provider other than FARGATE, I have to use CfnService instead of Ec2Service API. here


This is 🐛 Bug Report

@mostafafarzaneh mostafafarzaneh added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 2, 2021
@peterwoodworth peterwoodworth changed the title (ECS): Assign public IP is not supported for this launch type (aws-ecs): Assign public IP is not supported for this launch type Mar 2, 2021
@peterwoodworth peterwoodworth added the @aws-cdk/aws-ecs Related to Amazon Elastic Container label Mar 2, 2021
@SoManyHs SoManyHs added guidance Question that needs advice or information. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 5, 2021
@SoManyHs
Copy link
Contributor

SoManyHs commented Mar 5, 2021

Hi @mostafafarzaneh!

Do you have private and public subnets set up in the VPC you are trying to launch this in? It might be helpful to include the code you are using to spin up your ECS cluster, but this looks like you are trying to assign a a public IP address while using awsvpc network mode. However, as per the docs:

task ENIs are not given public IP addresses. To access the internet, tasks should be launched in a private subnet that is configured to use a NAT gateway.

Also, I would recommend using the Ec2Service construct rather than the raw CfnService construct, as that will come with more useful defaults and validations.

Let me know if that helps!

@SoManyHs SoManyHs added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 5, 2021
@mostafafarzaneh
Copy link
Author

this looks like you are trying to assign a a public IP address while using awsvpc network mode

Yes. I am trying to do this. Because Fargate is using awsvpc and can be assigned public IP to it, I thought it would be ok to set public Ip to Ec2 instances that using awsvpc.

Also, I would recommend using the Ec2Service construct rather than the raw CfnService construct, as that will come with more useful defaults and validations.

Because the Ec2Service construct does not allow me to assign a custom capacity provider I have to use CfnService construct. Take look at it here.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 7, 2021
@SoManyHs
Copy link
Contributor

SoManyHs commented Mar 9, 2021

Right, so unfortunately you cannot assign a public IP to services using the EC2 launch type and awsvpc networking mode.

Also, Autoscaling group capacity providers are not yet fully supported in CloudFormation -- you can follow the progress of that issue here. We also are tracking it on the ECS project board here.

Closing this issue for now, but please feel free to reopen if you have further questions!

@SoManyHs SoManyHs closed this as completed Mar 9, 2021
@github-actions
Copy link

github-actions bot commented Mar 9, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@LajosPolya
Copy link

LajosPolya commented May 9, 2023

@mostafafarzaneh
You can set the following:
Network Mode to NetworkMode.HOST
Leave assignPublicIp to false
Leave vpcSubnets as undefined
Leave securityGroups as []

You can find the public IP and the public DNS as follows:
AWS Console -> Amazon Elastic Container Service -> Clusters -> cluser_name -> Infrastructure -> Container instance -> container_instance_id -> Public IP or Public DNS respectively

An example can be found here:
https://github.com/LajosPolya/aws-cdk-templates/blob/main/deploy-ecs-with-ec2/lib/deploy-ecs-with-ec2-stack.ts#L9

This uses the container host's public IP.
More info can be found here under the "Using a public subnet and internet gateway" heading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

6 participants