How to use existing IAM.Role in AWS CDK

Hello Tech Lovers, I hope you are doing great. Do you want to use your existing IAM role in your AWS CDK script? But you are facing difficulties in the use of the existing role in your AWS CDK.

Must Read: How to add Multiple Lambdas in One AWS CDK?

Don’t worry today I am going to show you how you can use your existing “Iam.Role” in your CDK Script and will also let you know how you can get the ARN of your Existing Role using Command Prompt or Linux Terminal.

Normally we use ManagedPolicies using these lines of Code:

lambda_role = iam.Role(self, f"{ROLE_NAME}-Role", 
                    assumed_by=iam.ServicePrincipal('lambda.amazonaws.com'),
                managed_policies=[
iam.ManagedPolicy.from_aws_managed_policy_name('AmazonDynamoDBFullAccess'),
iam.ManagedPolicy.from_aws_managed_policy_name('AWSLambda_FullAccess')
                ])

But you don’t want to create new Role for each CDK Stack and you have created a Role and you want to use that existing that role in your all CDK Scripts.

Must Read: How to get Relationship properties values in Neo4J?

Simple you need to replace this code with:

lambda_role = iam.Role.from_role_arn(self, "your role name",

role_arn="your role arn", mutable=False)

Now you can use this existing role in all of your CDK scripts.

If you face any issues let me know in the comment section and you can also “Hire Me” for your AWS CDK tasks. Thank you

Leave a Reply