Giter Club home page Giter Club logo

amazon-eks-irsa-cfn's Introduction

Amazon EKS IAM Role for Service Accounts CDK/CloudFormation Library

This repository contains an AWS CloudFormation Custom Resource that creates an AWS IAM Role that is assumable by a Kubernetes Service Account. This role is known as an IRSA, or IAM Role for Service Account. This role can be associated with an Amazon EKS Cluster that you're creating in the same CloudFormation stack. Alternatively, the EKS Cluster can be created in a different stack and referenced by name.

For ease of implementation, this repository also contains a CDK Construct library you can import and use to easily create a Role. This is the quickest and most programmatic way to build the Role.

Alternatively, a SAM Template is available that you can use to deploy the Custom Resource Lambda Functions to your account and reference in your YAML or JSON CloudFormation templates.

CDK Construct Library usage

Install the Construct Library into your TypeScript project as follows:

npm install amazon-eks-irsa-cfn

In your source code, import the Construct classes:

import { Role, OIDCIdentityProvider } from 'amazon-eks-irsa-cfn';

Then declare the Constructs in your CDK Stack or Construct. The Role class implements IRole and can be used anywhere an IRole is needed.

See also https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iam.Role.html for a list of additional properties that can be supplied when instantiating a Role.

const provider = new OIDCIdentityProvider(this, 'Provider', {
    clusterName: 'MyCluster'
});

const role = new Role(this, 'Role', {
    clusterName: 'MyCluster',
    serviceAccount: 'myServiceAccount',
    namespace: 'default',
    // All other properties available in an `aws-iam.Role` class are available
    // e.g. `path`, `maxSessionDuration`, `description`, etc.
});

SAM Template and CloudFormation Custom Resources

There is a SAM Template located in the lambda-packages folder. It also properly associates the IAM Policies needed for the Lambda functions to execute properly.

To deploy it, you can run:

sam build
sam deploy

The Stack that is created by the Template exports the following values:

  • EKSIRSARoleCreationFunction - Role creation Lambda function ARN
  • OIDCIdentityProviderCreationFunction - OIDC identity provider creation Lambda function ARN

Once you've deployed the package, you can refer to the Lambda functions in your CloudFormation Stacks.

Here's an example Stack fragment that uses these functions to power Custom Resources:

Resources:
    MyIdentityProvider:
        Type: Custom::OIDCIdentityProvider
        Properties:
            ServiceToken: !ImportValue OIDCIdentityProviderCreationFunction
            ClusterName: MyCluster

    MyRole:
        Type: Custom::ServiceAccountRole
        Properties:
            ServiceToken: !ImportValue EKSIRSARoleCreationFunction
            ClusterName: MyCluster
            ServiceAccount: myServiceAccount
            # All other properties supported by AWS::IAM::Role can be
            # added here, like Description, Policies, etc.

License

This project is licensed under the Apache-2.0 License.

amazon-eks-irsa-cfn's People

Contributors

callms avatar dependabot[bot] avatar max-boehm avatar otterley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

amazon-eks-irsa-cfn's Issues

IRSA Custom Resource "delete role" operation fails

When using the IRSA Custom Resource in a CloudFormation template I get an error when deleting the stack. Example:

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  ClusterName:
    Description: EKS cluster name
    Type: String

Resources:
  IAMRole:
    Type: Custom::ServiceAccountRole
    Properties:
      RoleName: !Sub "${ClusterName}-default-role"
      ServiceToken: !ImportValue EKSIRSARoleCreationFunction
      ClusterName: !Ref ClusterName
      ServiceAccount: default
      Namespace: default
      ManagedPolicyArns:
        - !Ref IAMPolicy
  IAMPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: IAM policy for the service account
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action: s3:*
            Resource: '*'

When deleting the stack I get:

Received response status [FAILED] from custom resource. Message returned: Cannot delete entity, must detach all policies first. (RequestId: ef5d25cc-8883-4d0c-a24e-9ccb35a221b8)

I think the deleteRole() function in the lambda handler needs to detach the role policies first. I had created a PR with a fix, but it has not been discussed and was just closed, see #7.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.