|
| 1 | +AWSTemplateFormatVersion: 2010-09-09 |
| 2 | +Description: >- |
| 3 | + Password validation with a Lambda-backed custom resource |
| 4 | +
|
| 5 | +Parameters: |
| 6 | + Password: |
| 7 | + NoEcho: 'true' |
| 8 | + Description: Enter Password |
| 9 | + Type: String |
| 10 | + MinLength: '6' |
| 11 | + MaxLength: '10' |
| 12 | + AllowedPattern: '[a-zA-Z0-9]*' |
| 13 | + ConstraintDescription: alphanumeric characters. |
| 14 | + ConfirmPassword: |
| 15 | + NoEcho: 'true' |
| 16 | + Description: Confirm Password |
| 17 | + Type: String |
| 18 | + MinLength: '6' |
| 19 | + MaxLength: '10' |
| 20 | + AllowedPattern: '[a-zA-Z0-9]*' |
| 21 | + ConstraintDescription: alphanumeric characters. |
| 22 | + |
| 23 | +Metadata: |
| 24 | + 'AWS::CloudFormation::Interface': |
| 25 | + ParameterGroups: |
| 26 | + - Label: |
| 27 | + default: Confirm the password |
| 28 | + Parameters: |
| 29 | + - Password |
| 30 | + - ConfirmPassword |
| 31 | + |
| 32 | +Resources: |
| 33 | + LambdaExecutionRole: |
| 34 | + Type: AWS::IAM::Role |
| 35 | + Properties: |
| 36 | + AssumeRolePolicyDocument: |
| 37 | + Version: 2012-10-17 |
| 38 | + Statement: |
| 39 | + - Effect: Allow |
| 40 | + Principal: |
| 41 | + Service: |
| 42 | + - lambda.amazonaws.com |
| 43 | + Action: |
| 44 | + - 'sts:AssumeRole' |
| 45 | + Policies: |
| 46 | + - PolicyName: lambdalogtocloudwatch |
| 47 | + PolicyDocument: |
| 48 | + Version: 2012-10-17 |
| 49 | + Statement: |
| 50 | + - Effect: Allow |
| 51 | + Action: |
| 52 | + - 'logs:CreateLogGroup' |
| 53 | + - 'logs:CreateLogStream' |
| 54 | + - 'logs:PutLogEvents' |
| 55 | + Resource: 'arn:aws:logs:*:*:*' |
| 56 | + |
| 57 | + CheckPasswordsFunction: |
| 58 | + Type: AWS::Lambda::Function |
| 59 | + Properties: |
| 60 | + Code: |
| 61 | + ZipFile: | |
| 62 | + import json |
| 63 | +
|
| 64 | + import cfnresponse |
| 65 | +
|
| 66 | +
|
| 67 | + def lambda_handler(event, context): |
| 68 | + print(json.dumps(event)) |
| 69 | + response_data = {} |
| 70 | + response_data['Data'] = None |
| 71 | +
|
| 72 | + if event['RequestType'] != 'Create': |
| 73 | + cfnresponse.send(event, context, cfnresponse.SUCCESS, |
| 74 | + response_data, "CustomResourcePhysicalID") |
| 75 | + return |
| 76 | +
|
| 77 | + password = event['ResourceProperties']['Password'] |
| 78 | + confirm_password = event['ResourceProperties']['ConfirmPassword'] |
| 79 | +
|
| 80 | + if password == confirm_password: |
| 81 | + cfnresponse.send(event, context, cfnresponse.SUCCESS, |
| 82 | + response_data, "CustomResourcePhysicalID") |
| 83 | + else: |
| 84 | + print('Passwords do not match!') |
| 85 | + cfnresponse.send(event, context, cfnresponse.FAILED, |
| 86 | + response_data, "CustomResourcePhysicalID") |
| 87 | + |
| 88 | + Description: CloudFormation custom resource |
| 89 | + FunctionName: CheckPasswords |
| 90 | + Handler: index.lambda_handler |
| 91 | + Runtime: python3.7 |
| 92 | + Timeout: 3 |
| 93 | + Role: !GetAtt LambdaExecutionRole.Arn |
| 94 | + |
| 95 | + TestPasswords: |
| 96 | + Type: Custom::LambdaCallout |
| 97 | + Properties: |
| 98 | + ServiceToken: !GetAtt |
| 99 | + - CheckPasswordsFunction |
| 100 | + - Arn |
| 101 | + Password: !Ref Password |
| 102 | + ConfirmPassword: !Ref ConfirmPassword |
| 103 | + |
| 104 | + CFNUser: |
| 105 | + Type: AWS::IAM::User |
| 106 | + Properties: |
| 107 | + LoginProfile: |
| 108 | + Password: !Ref Password |
| 109 | + |
| 110 | + CFNUserGroup: |
| 111 | + Type: 'AWS::IAM::Group' |
| 112 | + |
| 113 | + CFNAdminGroup: |
| 114 | + Type: AWS::IAM::Group |
| 115 | + |
| 116 | + Users: |
| 117 | + Type: AWS::IAM::UserToGroupAddition |
| 118 | + Properties: |
| 119 | + GroupName: !Ref CFNUserGroup |
| 120 | + Users: |
| 121 | + - !Ref CFNUser |
| 122 | + |
| 123 | + Admins: |
| 124 | + Type: AWS::IAM::UserToGroupAddition |
| 125 | + Properties: |
| 126 | + GroupName: !Ref CFNAdminGroup |
| 127 | + Users: |
| 128 | + - !Ref CFNUser |
| 129 | + |
| 130 | + CFNUserPolicies: |
| 131 | + Type: AWS::IAM::Policy |
| 132 | + Properties: |
| 133 | + PolicyName: CFNUsers |
| 134 | + PolicyDocument: |
| 135 | + Statement: |
| 136 | + - Effect: Allow |
| 137 | + Action: |
| 138 | + - 'cloudformation:Describe*' |
| 139 | + - 'cloudformation:List*' |
| 140 | + - 'cloudformation:Get*' |
| 141 | + Resource: '*' |
| 142 | + Groups: |
| 143 | + - !Ref CFNUserGroup |
| 144 | + |
| 145 | + CFNAdminPolicies: |
| 146 | + Type: AWS::IAM::Policy |
| 147 | + Properties: |
| 148 | + PolicyName: CFNAdmins |
| 149 | + PolicyDocument: |
| 150 | + Statement: |
| 151 | + - Effect: Allow |
| 152 | + Action: 'cloudformation:*' |
| 153 | + Resource: '*' |
| 154 | + Groups: |
| 155 | + - !Ref CFNAdminGroup |
| 156 | + |
| 157 | + CFNKeys: |
| 158 | + Type: AWS::IAM::AccessKey |
| 159 | + Properties: |
| 160 | + UserName: !Ref CFNUser |
| 161 | + |
| 162 | +Outputs: |
| 163 | + AccessKey: |
| 164 | + Value: !Ref CFNKeys |
| 165 | + Description: AWSAccessKeyId of new user |
| 166 | + SecretKey: |
| 167 | + Value: !GetAtt CFNKeys.SecretAccessKey |
| 168 | + Description: AWSSecretKey of new user |
0 commit comments