This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathec2-iamrole.yaml
119 lines (119 loc) · 3.1 KB
/
ec2-iamrole.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS in Action: chapter 6 (IAM role)'
Parameters:
KeyName:
Description: 'Key Pair name'
Type: 'AWS::EC2::KeyPair::KeyName'
Default: mykey
VPC:
Description: 'Just select the one and only default VPC'
Type: 'AWS::EC2::VPC::Id'
Subnet:
Description: 'Just select one of the available subnets'
Type: 'AWS::EC2::Subnet::Id'
Lifetime:
Description: 'Lifetime in minutes (2-59)'
Type: Number
Default: '2'
MinValue: '2'
MaxValue: '59'
Mappings:
RegionMap:
'ap-south-1':
AMI: 'ami-2ed19c41'
'eu-west-3':
AMI: 'ami-c8a017b5'
'eu-west-2':
AMI: 'ami-e3051987'
'eu-west-1':
AMI: 'ami-760aaa0f'
'ap-northeast-2':
AMI: 'ami-fc862292'
'ap-northeast-1':
AMI: 'ami-2803ac4e'
'sa-east-1':
AMI: 'ami-1678037a'
'ca-central-1':
AMI: 'ami-ef3b838b'
'ap-southeast-1':
AMI: 'ami-dd7935be'
'ap-southeast-2':
AMI: 'ami-1a668878'
'eu-central-1':
AMI: 'ami-e28d098d'
'us-east-1':
AMI: 'ami-6057e21a'
'us-east-2':
AMI: 'ami-aa1b34cf'
'us-west-1':
AMI: 'ami-1a033c7a'
'us-west-2':
AMI: 'ami-32d8124a'
Resources:
SecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'My security group'
VpcId: !Ref VPC
SecurityGroupIngress:
- CidrIp: '0.0.0.0/0'
FromPort: 22
IpProtocol: tcp
ToPort: 22
Tags:
- Key: Name
Value: 'AWS in Action: chapter 6 (IAM role)'
InstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Roles:
- !Ref Role
Role:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'ec2.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: ec2
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: Stmt1425388787000
Effect: Allow
Action:
- 'ec2:StopInstances'
Resource:
- '*'
Condition:
StringEquals:
'ec2:ResourceTag/aws:cloudformation:stack-id': !Ref 'AWS::StackId'
Instance:
Type: 'AWS::EC2::Instance'
Properties:
IamInstanceProfile: !Ref InstanceProfile
ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
InstanceType: 't2.micro'
KeyName: !Ref KeyName
SecurityGroupIds:
- !Ref SecurityGroup
SubnetId: !Ref Subnet
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -x
INSTANCEID="$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
echo "aws ec2 stop-instances --instance-ids $INSTANCEID --region ${AWS::Region}" | at now + ${Lifetime} minutes
Tags:
- Key: Name
Value: 'AWS in Action: chapter 6 (IAM role)'
Outputs:
InstancePublicName:
Value: !Sub ${Instance.PublicDnsName}
Description: Public name (connect via SSH as user ec2-user)