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-eks] Fail to create FargateProfile - Missing permissions for ec2:DescribeSubnets #7614

Closed
pahud opened this issue Apr 27, 2020 · 8 comments · Fixed by #7706
Closed

[aws-eks] Fail to create FargateProfile - Missing permissions for ec2:DescribeSubnets #7614

pahud opened this issue Apr 27, 2020 · 8 comments · Fixed by #7706
Assignees
Labels
@aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service bug This issue is a bug.

Comments

@pahud
Copy link
Contributor

pahud commented Apr 27, 2020

We just annouced the Fargate support for Amazon EKS in 4 additional regions and I was trying to deploy a simple EKS+Fargate cluster to them ended up missiong permissions failure in eu-central-1, ap-southeast-1 and ap-southeast-2.

Reproduction Steps

import * as cdk from '@aws-cdk/core';
import * as eks from '@aws-cdk/aws-eks';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';

export class EksfgStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, 'Vpc', {
        maxAzs: 3,
        natGateways: 1
      });

    const mastersRole = new iam.Role(this, 'AdminRole', {
      assumedBy: new iam.AccountRootPrincipal()
    });

    const cluster = new eks.Cluster(this, 'Cluster', {
      vpc,
      mastersRole
    });

    cluster.addFargateProfile('FargateProfile', {
      selectors: [
        { namespace: 'default' },
        { namespace: 'kube-system' },
      ]
    })

    new cdk.CfnOutput(this, 'Region', { value: this.region })

  }
}

Error Log

Cluster/fargate-profile-FargateProfile/Resource/Default (ClusterfargateprofileFargateProfileA6BADBA5) Failed to create resource. Error: Missing permissions for `ec2:DescribeSubnets` action

圖片

Environment

  • CLI Version : 1.35.0
  • Framework Version: 1.35.0
  • OS : Mac OS X
  • Language : Typescript

Other


This is 🐛 Bug Report

@pahud pahud added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 27, 2020
@river0825
Copy link

river0825 commented Apr 27, 2020

+1
I have the same issue too

Reproduction Steps

import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as eks from '@aws-cdk/aws-eks';


export class CdkPracticeStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    
    
    const vpc = new ec2.Vpc(this, 'TheVPC', {
      cidr: "10.0.0.0/16"
    })
    
    
    const cluster = new eks.FargateCluster(this, 'hello-eks', {
      clusterName: "cdk-practice",
      vpc: vpc
      // defaultCapacityInstance: new ec2.InstanceType("t3.micro")
    });
    
    cluster.addResource('mypod', {
      apiVersion: 'v1',
      kind: 'Pod',
      metadata: { name: 'mypod' },
      spec: {
        containers: [
          {
            name: 'hello',
            image: 'paulbouwer/hello-kubernetes:1.5',
            ports: [ { containerPort: 8080 } ]
          }
        ]
      }
    });
    // The code that defines your stack goes here
  }
}

Environment

  • Cloud 9
  • CLI Version : 1.35.0
  • Framework Version: 1.35.0
  • Language: Typescript
  • Region: ap-southeast-1

Error

dev-ops_practice_-_AWS_Cloud9

@pahud
Copy link
Contributor Author

pahud commented Apr 27, 2020

this is weird I can successfully deploy it in us-west-2 but all failed in the other 3 regions.

@SomayaB SomayaB added the @aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service label Apr 29, 2020
@eduardomourar
Copy link
Contributor

I can confirm the same behavior while creating the fargate profile in region eu-central-1. A workaround for that is to add ec2:* policy to the cluster creation role:

    const cluster = new eks.Cluster(this, 'Cluster', {
      vpc,
      mastersRole
    })

    const clusterResource = cluster.node.defaultChild as cdk.CfnResource
    const clusterCreationRole = clusterResource.node.tryFindChild('CreationRole') as iam.Role
    clusterCreationRole.addToPolicy(new iam.PolicyStatement({
      actions: [ 'ec2:*' ],
      resources: [ '*' ],
    }))

@pahud
Copy link
Contributor Author

pahud commented Apr 30, 2020

@eduardomourar thanks for the feedback. As Fargate profile would only associate with private subnets, if no subnetSelection property provided for the FargateProfile resource, the creation role will try to figure out the private subnet IDs hense require extra permissions such as ec2:DescribeSubnets and it looks like it would require more than that. In this case, if we create the EKS cluster with a provided VPC which has no private subnets and we don't specify subnets for the Cluster resource, the cluster will still be created with no error with all public subnets associated with it, but the Fargate Profile, on the other hand, will not be able to be created as all subnets associated with this cluster are pubilc only.

PR underway.

@pahud
Copy link
Contributor Author

pahud commented Apr 30, 2020

After checking the cloudtrail logs, the creation role will also need ec2:DescribeRouteTables to determine if there's any private subnets.

@pahud
Copy link
Contributor Author

pahud commented Apr 30, 2020

Now the question is, how can we determine if there are private subnets associated with this cluster? If we can check this, we can throw errors and avoid this error:

圖片

Wondering if we can do this way

const privateSubnets = cluster.vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE }).subnetIds
if (!privateSubnets || privateSubnets.length === 0) {
      throw new Error('Fargate profile requires at least one private subnet but no private subnets found from the subnetSelection')
}

@eladb any comments?

@eladb
Copy link
Contributor

eladb commented Apr 30, 2020

Can we just add those permissions always?

@pahud
Copy link
Contributor Author

pahud commented Apr 30, 2020

Can we just add those permissions always?

Yes, looks like adding the additional ec2:DescribeRouteTables will fix this issue. I am testing manually in different regions. Will create a PR for it today.

@SomayaB SomayaB added in-progress This issue is being actively worked on. and removed needs-triage This issue or PR still needs to be triaged. labels Apr 30, 2020
@mergify mergify bot closed this as completed in #7706 May 3, 2020
mergify bot pushed a commit that referenced this issue May 3, 2020
Creating fargate profile without specifying `subnetSelection` will require extra iam policy to allow the iam role to describe subnets and route tables to select private subnets.  This PR adds the required `ec2:DescribeRouteTables` for the cluster creation role.

Closes #7614
@iliapolo iliapolo changed the title Fail to create FargateProfile - Missing permissions for ec2:DescribeSubnets [aws-eks] Fail to create FargateProfile - Missing permissions for ec2:DescribeSubnets Aug 16, 2020
@iliapolo iliapolo removed the in-progress This issue is being actively worked on. label Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service bug This issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants