AWSTemplateFormatVersion: "2010-09-09"
Description: >
  Broken Labs - IAM Lab 06.
  An automation role needs to create Lambda functions with an execution role, but creation fails with a permissions error.
  Licensed under CC BY-NC-SA 4.0: https://creativecommons.org/licenses/by-nc-sa/4.0/
  Developed by: Mark Schoonover <mark.schoonover@gmail.com>

Resources:

  LabTargetRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "brokenlabs-iam-lab-06-target-${AWS::Region}-${AWS::AccountId}"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  LabActorRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "brokenlabs-iam-lab-06-actor-${AWS::Region}-${AWS::AccountId}"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: LabActorPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Sid: AllowCreateFunction
                Effect: Allow
                Action:
                  - lambda:CreateFunction
                  - lambda:GetFunction
                Resource: "*"
              - Sid: AllowPassRole
                Effect: Allow
                Action: iam:PassRole
                Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/brokenlabs-iam-lab-06-other-${AWS::Region}-${AWS::AccountId}"

Outputs:

  RoleName:
    Description: Name of the IAM role to test in the Policy Simulator
    Value: !Ref LabActorRole

  TestResourceArn:
    Description: Resource ARN to simulate iam:PassRole against (the role the actor needs to pass to Lambda)
    Value: !GetAtt LabTargetRole.Arn

  TargetRoleName:
    Description: Name of the role the actor needs to pass
    Value: !Ref LabTargetRole
