---
AWSTemplateFormatVersion: "2010-09-09"
Description: "IAM instance role"
Parameters:
RoleName:
Type: String
Description: Role name.
RoleProfileName:
Type: String
Description: Role profile name.
Id:
Type: String
Description: Unique identifier, prefix for all resources created below.
KmsKeyArn:
Type: String
Description: KMS key ARN that de/encrypts resources.
S3BucketName:
Type: String
Description: S3 bucket name to store.
Mappings:
ServicePrincipals:
aws-cn:
ec2: ec2.amazonaws.com.cn
aws:
ec2: ec2.amazonaws.com
Resources:
InstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Ref RoleName
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- Fn::FindInMap:
- ServicePrincipals
- Ref: AWS::Partition
- ec2
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMFullAccess
- arn:aws:iam::aws:policy/CloudWatchFullAccess
Path: /
Policies:
- PolicyName: avalanched-instance-role-policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ec2:DescribeInstances - ec2:DescribeTags - ec2:DescribeVolumes - ec2:CreateTags
- ec2:CreateVolume - ec2:AttachVolume
- ec2:DetachVolume - autoscaling:SetInstanceHealth - ec2:TerminateInstances Resource: "*"
- Effect: Allow
Action:
- kms:Encrypt - kms:GenerateDataKey* - kms:DescribeKey Resource: { Ref: KmsKeyArn }
- Effect: Allow
Action:
- s3:List*
Resource: "*"
- Effect: Allow
Action:
- s3:GetObject - s3:PutObject Resource:
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/bootstrap/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/pki/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/discover/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/backups/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/events/*",
],
]
- !Join [
"",
[
!Sub "arn:${AWS::Partition}:s3:::",
!Ref S3BucketName,
"/",
!Ref Id,
"/ssm-output-logs/*",
],
]
- Effect: Allow
Action:
- cloudwatch:PutMetricData
Resource: "*"
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogStreams
- logs:PutRetentionPolicy
Resource:
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${Id}"
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${Id}:log-stream:*"
- Effect: Allow
Action:
- ec2:AllocateAddress - ec2:AssociateAddress - ec2:DescribeAddresses Resource: "*"
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: !Ref RoleProfileName
Path: "/"
Roles:
- !Ref InstanceRole
Outputs:
InstanceRoleArn:
Value: !GetAtt InstanceRole.Arn
Description: Role ARN
InstanceProfileArn:
Value: !GetAtt InstanceProfile.Arn
Description: Instance profile ARN