![]() |
In this document, we will demonstrate how to set up aws-cdk in Windows (with additional helpful links provided for MAC or Linux users). Using Java as the coding language, we will build and deploy changes to create a new IAM user. Most of the operations will be automated using code with minimal interaction on the AWS UI for initial setup. What is aws-cdk?AWS-CDK, short for Amazon Web Services Cloud Development Kit, is an open-source software development framework for building applications on AWS. It allows you to write infrastructure-related code using your preferred programming language, supported by aws-cdk. This code is then transformed into a CloudFormation template and deployed on AWS. A typical workflow using aws-cdk involves writing code for AWS infrastructure, running ‘cdk synth’ in the command line interface to generate a JSON template, and then running ‘cdk deploy’ to deploy the infrastructure on AWS. What is IAM and IAM user in AWS?IAM stands for Identity and Access Management. It governs access to resources, determining which parts of the resources can be accessed and the specific ways they can be accessed (e.g., read, write). An IAM user can be an individual or a group with assigned access privileges, authorized to log in using their credentials. They don’t have their own resources or account; instead, they utilize a portion of the resources within the owner’s AWS account, based on the permissions granted to them. What is AWS CLI? How it’s linked to aws-cdk?AWS-CLI is a tool that performs similar tasks to aws-cdk but uses commands to manage our AWS services. In the current workflow, it helps configure and authenticate users to deploy aws-cdk related code. This is a one-time intial step. What is access key?Access keys are credentials that allow us to securely access AWS resources. In our tutorial, they will enable us to automatically deploy our code by authenticating at the backend (we will set this up initially). They can be used not only for aws-cdk, but also for AWS CLI or AWS API. During access key creation, an access key ID and a secret access key should be noted. Both should be securely stored as they will not be visible later on. Why using aws-cdk?Pros:
Cons:
Inline VS ManagedPoliciesInline policies cannot be attached to more than one user, role, or group. They are unique to one IAM entity unlike ManagedPolicies.
How to create Inline Policies and attach to an IAM user: PolicyDocument policyDocument = PolicyDocument.Builder.create()
.statements(List.of(
PolicyStatement.Builder.create()
.effect(Effect.ALLOW)
.actions(Collections.singletonList("iam:ChangePassword"))
.resources(Collections.singletonList("*"))
.build()
))
.build();
Policy inlinePolicy = new Policy(this, "MyInlinePolicy", PolicyProps.builder()
.policyName("MyInlinePolicy")
.document(policyDocument)
.build());
//Assigning Inline policy
testUser.attachInlinePolicy(inlinePolicy); How to assign group to an IAM user: //Creating group
Group group = Group.Builder.create(this, "MyGroup") .groupName("MyGroup").managedPolicies(List.of(ManagedPolicy.fromAwsManagedPolicyName("AmazonS3ReadOnlyAccess"))) .build();
// Assigning group to a user
testUser.addToGroup(group);
//Multiple groups can be assigned by using the same statement with another group
testUser.addToGroup(group1);
testUser.addToGroup(group2); Creating access key in AWS
Setting up AWS CLI
aws --version Above command would help in confirming if the installation happened properly. Configuring credentials for AWS1. Run: aws configure 2. Put access key (captured from credentials), passcode (captured from credentials), region, and output format. aws sts get-caller-identity What is the region and output format?
Installing aws-cdk1. Run following to create a directory for project (Remember the name that you give here will be taken as the project name): mkdir my-first-proj
cd my-first-proj 2. Run following command to install required libraries: npm install -g aws-cdk 3. Run following command to create initial maven project for starting: cdk init app --language java ![]() installing and init Setting up the development environment for aws-cdk with Java:Prerequisites: JDK 17 and IntelliJ (or any other IDE supporting Maven) already installed in your machine.
![]() Project Skeleton after import Attaching policies and permissions to an IAM user:1) Creating a managed Policy using ManagedPolicy object: Way 1: ManagedPolicy managedPolicy1=new ManagedPolicy(this, "managedPolicy-app_name-environment_name1-region"
, ManagedPolicyProps.builder()
.managedPolicyName("managedPolicy-app_name-environment_name1-region-01")
.statements(List.of(
PolicyStatement.Builder.create()
.effect(Effect.ALLOW)
.actions(Collections.singletonList("iam:ChangePassword"))
.resources(Collections.singletonList("*"))
.build()
)).build()) Explanation: These policies are created and customized based on the user creating them. We have more control over it. Way2: IManagedPolicy managedPolicy2 = ManagedPolicy.fromAwsManagedPolicyName("ReadOnlyAccess") Explanation: These are policies predefined and managed by AWS for ready-to-use and we don’t have to define any PolicyStatement for this explicitly. 2) Attaching it to User (or role or group) .managedPolicies(List.of(managedPolicy1,managedPolicy2)) Java aws-cdk code to define Environment and Stack:
Java aws-cdk code to define an IAM user
Deploying to AWS1. To compare what changes will happen, run: cdk diff ![]() Comparision with actual changes present on aws 2. To validate json template generated for deployment, run. cdk synth 3. Final command to deploy changes, run cdk deploy ![]() Deploying to aws Verifying on AWS1) Verifying user creation:
![]() IAM user created 2) Verifying Policies creation Navigate to: IAM -> Users -> [Name of the user created] -> (Scroll down) Permission Policies. ![]() Policies attached to IAM user Best practices to manage IAM user credentials securely
Best Coding practices
Great!! You did it! Using AWS-CDK (Java) to automate creation of IAM User – FAQsHow to add more permissions to the user?
Can we create more users?
Do we need any other setup to do other operations?
Can we see how JSON templates are getting created for whatever code we are writing on the AWS portal?
Can we automate the deployment of code using the CI/CD pipeline?
|
Reffered: https://www.geeksforgeeks.org
Amazon Web Services |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |