Question:
I was editing my CloudFormation templates and suddenly AWS tells me I need CAPABILITY_NAMED_IAM
. I am curious as to which change triggers this?
What is a named IAM resource?
Before I already “name” my resources like
1 2 |
RoleName: !Sub '${PipelineName}-codebuild' |
I am not asked to add this capability, I think until I add
1 2 3 4 5 6 7 8 9 10 11 |
Parameters: AppName: Type: String Description: Prefix for resources Resources: LambdaRole: Type: AWS::IAM::Role Properties: RoleName: !Ref AppName |
To my SAM application template. But arent they the “same” except one uses !Ref
? Or maybe some other change triggered this?
For reference, my CodePipeline stack
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
AWSTemplateFormatVersion : '2010-09-09' Description: 'Skynet stack for CodePipeline' Parameters: PipelineName: Type: String Description: Pipeline Name (Lower case only, since S3 bucket names can only have lowercase) Default: skynet-pipeline GitHubOwner: Type: String Description: GitHub Owner Default: 2359media GitHubRepo: Type: String Description: GitHub Repo Default: 'skynet' GitHubBranch: Type: String Description: GitHub Branch Default: master GitHubToken: Type: String Description: GitHub Token NoEcho: true Resources: Pipeline: Type: AWS::CodePipeline::Pipeline Properties: Name: !Ref PipelineName RoleArn: !GetAtt [PipelineRole, Arn] ArtifactStore: Location: !Ref PipelineArtifactStore Type: S3 DisableInboundStageTransitions: [] Stages: - Name: GitHubSource Actions: - Name: Source ActionTypeId: Category: Source Owner: ThirdParty Version: 1 Provider: GitHub Configuration: Owner: !Ref GitHubOwner Repo: !Ref GitHubRepo Branch: !Ref GitHubBranch OAuthToken: !Ref GitHubToken OutputArtifacts: - Name: SourceCode - Name: Build Actions: - Name: Lambda InputArtifacts: - Name: SourceCode OutputArtifacts: - Name: LambdaPackage ActionTypeId: Category: Build Owner: AWS Version: 1 Provider: CodeBuild Configuration: ProjectName: !Ref CodeBuildLambda - Name: CreateChangeSet Actions: - Name: Lambda InputArtifacts: - Name: LambdaPackage OutputArtifacts: - Name: LambdaDeployment ActionTypeId: Category: Deploy Owner: AWS Version: 1 Provider: CloudFormation Configuration: ActionMode: CHANGE_SET_REPLACE ChangeSetName: !Sub - '${PipelineName}-lambda' - {PipelineName: !Ref PipelineName} RoleArn: !GetAtt [CloudFormationRole, Arn] StackName: !Sub - '${PipelineName}-lambda' - {PipelineName: !Ref PipelineName} TemplatePath: 'LambdaPackage::SkynetLambdaPackaged.yml' Capabilities: CAPABILITY_NAMED_IAM ParameterOverrides: !Sub '{"AppName": "${PipelineName}-lambda"}' - Name: ExecuteChangeSet Actions: - Name: Lambda ActionTypeId: Category: Deploy Owner: AWS Version: 1 Provider: CloudFormation Configuration: ActionMode: CHANGE_SET_EXECUTE ChangeSetName: !Sub - '${PipelineName}-lambda' - {PipelineName: !Ref PipelineName} StackName: !Sub - '${PipelineName}-lambda' - {PipelineName: !Ref PipelineName} CodeBuildLambda: Type: AWS::CodeBuild::Project Properties: Name: !Sub '${PipelineName}-lambda' Artifacts: Type: CODEPIPELINE Environment: ComputeType: BUILD_GENERAL1_SMALL Image: aws/codebuild/nodejs:7.0.0 Type: LINUX_CONTAINER EnvironmentVariables: - Name: S3_BUCKET Value: !Ref PipelineArtifactStore ServiceRole: !Ref CodeBuildRole Source: BuildSpec: 'lambda/buildspec.yml' Type: CODEPIPELINE PipelineArtifactStore: Type: AWS::S3::Bucket Properties: BucketName: !Sub '${PipelineName}-artifacts' VersioningConfiguration: Status: Enabled CodeBuildRole: Type: AWS::IAM::Role Properties: RoleName: !Sub '${PipelineName}-codebuild' AssumeRolePolicyDocument: Version: '2012-10-17' Statement: Effect: Allow Principal: Service: codebuild.amazonaws.com Action: sts:AssumeRole Policies: - PolicyName: !Sub '${PipelineName}-codebuild' PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Resource: 'arn:aws:logs:*:*:*' Action: - 'logs:CreateLogGroup' - 'logs:CreateLogStream' - 'logs:PutLogEvents' - Effect: Allow Resource: - !Sub 'arn:aws:s3:::codepipeline-${AWS::Region}-*/*' - !Sub - '${PipelineArtifactStoreArn}/*' - {PipelineArtifactStoreArn: !GetAtt [PipelineArtifactStore, Arn]} Action: - 's3:GetObject' - 's3:GetObjectVersion' - 's3:PutObject' CloudFormationRole: Type: AWS::IAM::Role Properties: RoleName: !Sub '${PipelineName}-cloudformation' AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: cloudformation.amazonaws.com Action: - sts:AssumeRole ManagedPolicyArns: - 'arn:aws:iam::aws:policy/AWSLambdaExecute' Policies: - PolicyName: !Sub '${PipelineName}-cloudformation' PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Resource: '*' Action: - 's3:GetObject' - 's3:GetObjectVersion' - 's3:GetBucketVersioning' - Effect: Allow Resource: 'arn:aws:s3:::codepipeline*' Action: - 's3:PutObject' - Effect: Allow Resource: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:*' Action: - 'lambda:*' - Effect: Allow Resource: !Sub 'arn:aws:apigateway:${AWS::Region}::*' Action: - 'apigateway:*' - Effect: Allow Resource: '*' Action: - 'lambda:CreateEventSourceMapping' - 'lambda:DeleteEventSourceMapping' - 'lambda:GetEventSourceMapping' - Effect: Allow Resource: '*' Action: - 'iam:GetRole' - 'iam:CreateRole' - 'iam:DeleteRole' - 'iam:PassRole' - 'iam:AttachRolePolicy' - 'iam:DetachRolePolicy' - 'iam:DeleteRolePolicy' - 'iam:PutRolePolicy' - Effect: Allow Resource: '*' Action: - 'iam:PassRole' - Effect: Allow Resource: !Sub 'arn:aws:cloudformation:${AWS::Region}:aws:transform/Serverless-2016-10-31' Action: - 'cloudformation:CreateChangeSet' PipelineRole: Type: AWS::IAM::Role Properties: RoleName: !Sub '${PipelineName}-pipeline' AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Action: ['sts:AssumeRole'] Effect: Allow Principal: Service: [codepipeline.amazonaws.com] Path: / Policies: - PolicyName: SkynetPipeline PolicyDocument: Version: '2012-10-17' Statement: - Action: - 's3:GetObject' - 's3:GetObjectVersion' - 's3:GetBucketVersioning' Effect: 'Allow' Resource: '*' - Action: - 's3:PutObject' Effect: 'Allow' Resource: - !GetAtt [PipelineArtifactStore, Arn] - Action: - 'codecommit:CancelUploadArchive' - 'codecommit:GetBranch' - 'codecommit:GetCommit' - 'codecommit:GetUploadArchiveStatus' - 'codecommit:UploadArchive' Effect: 'Allow' Resource: '*' - Action: - 'codedeploy:CreateDeployment' - 'codedeploy:GetApplicationRevision' - 'codedeploy:GetDeployment' - 'codedeploy:GetDeploymentConfig' - 'codedeploy:RegisterApplicationRevision' Effect: 'Allow' Resource: '*' - Action: - 'elasticbeanstalk:*' - 'ec2:*' - 'elasticloadbalancing:*' - 'autoscaling:*' - 'cloudwatch:*' - 's3:*' - 'sns:*' - 'cloudformation:*' - 'rds:*' - 'sqs:*' - 'ecs:*' - 'iam:PassRole' Effect: 'Allow' Resource: '*' - Action: - 'lambda:InvokeFunction' - 'lambda:ListFunctions' Effect: 'Allow' Resource: '*' - Action: - 'opsworks:CreateDeployment' - 'opsworks:DescribeApps' - 'opsworks:DescribeCommands' - 'opsworks:DescribeDeployments' - 'opsworks:DescribeInstances' - 'opsworks:DescribeStacks' - 'opsworks:UpdateApp' - 'opsworks:UpdateStack' Effect: 'Allow' Resource: '*' - Action: - 'cloudformation:CreateStack' - 'cloudformation:DeleteStack' - 'cloudformation:DescribeStacks' - 'cloudformation:UpdateStack' - 'cloudformation:CreateChangeSet' - 'cloudformation:DeleteChangeSet' - 'cloudformation:DescribeChangeSet' - 'cloudformation:ExecuteChangeSet' - 'cloudformation:SetStackPolicy' - 'cloudformation:ValidateTemplate' - 'iam:PassRole' Effect: 'Allow' Resource: '*' - Action: - 'codebuild:BatchGetBuilds' - 'codebuild:StartBuild' Effect: 'Allow' Resource: '*' |
The part of SAM stack (sam.yml
) changed recently
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 |
AWSTemplateFormatVersion : '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: 'Skynet. AWS Management Assistant' Parameters: AppName: Type: String Description: Prefix for resources Resources: LambdaRole: Type: AWS::IAM::Role Properties: RoleName: !Ref AppName AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com - apigateway.amazonaws.com Action: - sts:AssumeRole ManagedPolicyArns: - 'arn:aws:iam::aws:policy/AmazonEC2FullAccess' - 'arn:aws:iam::aws:policy/AWSLambdaFullAccess' - 'arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess' - 'arn:aws:iam::aws:policy/AmazonAPIGatewayInvokeFullAccess' - 'arn:aws:iam::aws:policy/CloudWatchLogsFullAccess' |
Answer:
When are CAPABILITY_IAM
/CAPABILITY_NAMED_IAM
Required
According to CloudFormation CreateStack Parameters, one of these is required when your Template includes any of the following resource types:
1 2 3 4 5 6 7 8 |
AWS::IAM::AccessKey AWS::IAM::Group AWS::IAM::InstanceProfile AWS::IAM::Policy AWS::IAM::Role AWS::IAM::User AWS::IAM::UserToGroupAddition |
When to use CAPABILITY_NAMED_IAM
instead of CAPABILITY_IAM
When any of your IAM resources have a custom name, such as a RoleName
then CAPABILITY_NAMED_IAM
is required.
Why are these required?
The Capabilities are there to ensure you realize that you’re creating IAM resources, that these will modify the permissions on your account, and that you have reviewed these resources and their permissions as necessary.