-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from atulgoel126/main-week3-day3-4
Adding a custom IAM policy for the EC2 instance with minimum permissions
- Loading branch information
Showing
5 changed files
with
60 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import * as ec2 from 'aws-cdk-lib/aws-ec2'; | ||
import { Construct } from 'constructs'; | ||
import {IRole} from "aws-cdk-lib/aws-iam"; | ||
|
||
interface SecureEC2InstanceProps { | ||
vpc: ec2.IVpc; | ||
role?: IRole; | ||
} | ||
|
||
export class SecureEC2Instance extends Construct { | ||
public readonly instance: ec2.Instance; | ||
|
||
constructor(scope: Construct, id: string, vpc: ec2.IVpc) { | ||
constructor(scope: Construct, id: string, props: SecureEC2InstanceProps) { | ||
super(scope, id); | ||
|
||
this.instance = new ec2.Instance(this, 'SecureInstance', { | ||
vpc, | ||
vpc: props.vpc, | ||
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO), | ||
machineImage: new ec2.AmazonLinuxImage(), | ||
role: props.role, // Use the provided role or let CDK create a default one | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as cdk from 'aws-cdk-lib'; | ||
import * as iam from 'aws-cdk-lib/aws-iam'; | ||
import { Construct } from 'constructs'; | ||
|
||
export class IamStack extends cdk.Stack { | ||
public readonly ec2Role: iam.Role; | ||
|
||
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
// Create EC2 role with the least privilege | ||
this.ec2Role = new iam.Role(this, 'EC2LeastPrivilegeRole', { | ||
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), | ||
description: 'Least privilege role for EC2 instances', | ||
}); | ||
|
||
// Add minimal required permissions | ||
this.ec2Role.addToPolicy(new iam.PolicyStatement({ | ||
effect: iam.Effect.ALLOW, | ||
actions: [ | ||
'ec2:DescribeTags', | ||
'ec2:DescribeInstances', | ||
'cloudwatch:PutMetricData' | ||
], | ||
resources: ['*'], | ||
})); | ||
|
||
// Export the role ARN | ||
new cdk.CfnOutput(this, 'EC2RoleArn', { | ||
value: this.ec2Role.roleArn, | ||
description: 'ARN of the EC2 least privilege role', | ||
exportName: 'EC2LeastPrivilegeRoleArn', | ||
}); | ||
} | ||
} |