Guest User

Untitled

a guest
Apr 8th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. {
  2. "AWSTemplateFormatVersion": "2010-09-09",
  3. "Metadata": {
  4. "AWS::CloudFormation::Interface": {
  5. "ParameterGroups": [
  6. {
  7. "Label": {
  8. "default": "HelloWorld Parameters"
  9. },
  10. "Parameters": [
  11. "PanoUsername",
  12. "PanoPassword",
  13. "LambdaRoleName"
  14. ]
  15. }
  16. ]
  17. }
  18. },
  19. "Parameters": {
  20. "PanoUsername": {
  21. "Description": "PanoUsername",
  22. "Type": "String",
  23. "NoEcho": true
  24. },
  25. "PanoPassword": {
  26. "Description": "PanoPassword",
  27. "Type": "String",
  28. "NoEcho": true
  29. }
  30. },
  31. "Resources": {
  32. "SSMPanoUser": {
  33. "Type": "AWS::SSM::Parameter",
  34. "Properties": {
  35. "Name": "pano-username",
  36. "Type": "String",
  37. "Value": {
  38. "Ref": "PanoUsername"
  39. },
  40. "Description": "SSM Parameter for Pano UserName",
  41. "AllowedPattern": "^[a-zA-Z]{1,10}$"
  42. }
  43. },
  44. "SSMPanoPassword": {
  45. "Type": "AWS::SSM::Parameter",
  46. "Properties": {
  47. "Name": "pano-password",
  48. "Type": "String",
  49. "Value": {
  50. "Ref": "PanoPassword"
  51. },
  52. "Description": "SSM Parameter for Password",
  53. "AllowedPattern": "^[a-zA-Z]{1,10}$"
  54. }
  55. },
  56. "LambdaExecutionRole": {
  57. "Type": "AWS::IAM::Role",
  58. "Properties": {
  59. "RoleName": {
  60. "Fn::Join": [
  61. "-",
  62. [
  63. "LambdaExecutionRole",
  64. {
  65. "Ref": "AWS::StackName"
  66. }
  67. ]
  68. ]
  69. },
  70. "AssumeRolePolicyDocument": {
  71. "Version": "2012-10-17",
  72. "Statement": [
  73. {
  74. "Effect": "Allow",
  75. "Principal": {
  76. "Service": [
  77. "lambda.amazonaws.com"
  78. ]
  79. },
  80. "Action": [
  81. "sts:AssumeRole"
  82. ]
  83. }
  84. ]
  85. },
  86. "Path": "/"
  87. }
  88. },
  89. "LambdaExecutionPolicy": {
  90. "Type": "AWS::IAM::ManagedPolicy",
  91. "DependsOn": [
  92. "LambdaExecutionRole"
  93. ],
  94. "Properties": {
  95. "ManagedPolicyName": {
  96. "Fn::Join": [
  97. "-",
  98. [
  99. "LambdaExecutionPolicy",
  100. {
  101. "Ref": "AWS::StackName"
  102. }
  103. ]
  104. ]
  105. },
  106. "Roles": [
  107. {
  108. "Ref": "LambdaExecutionRole"
  109. }
  110. ],
  111. "PolicyDocument": {
  112. "Version": "2012-10-17",
  113. "Statement": [
  114. {
  115. "Sid": "VisualEditor0",
  116. "Effect": "Allow",
  117. "Action": [
  118. "sqs:DeleteMessage",
  119. "dynamodb:PutItem",
  120. "states:ListExecutions",
  121. "dynamodb:DeleteItem",
  122. "ssm:Get*",
  123. "sqs:ReceiveMessage",
  124. "ec2:DeleteNetworkInterface",
  125. "sqs:SendMessage",
  126. "dynamodb:Scan",
  127. "dynamodb:Query",
  128. "dynamodb:UpdateItem",
  129. "sqs:GetQueueAttributes",
  130. "logs:CreateLogGroup",
  131. "logs:PutLogEvents",
  132. "ec2:CreateNetworkInterface",
  133. "logs:CreateLogStream",
  134. "ec2:DescribeNetworkInterfaces",
  135. "dynamodb:DescribeTable",
  136. "ssm:Describe*",
  137. "dynamodb:GetItem",
  138. "states:StartExecution",
  139. "ssm:List*",
  140. "dynamodb:UpdateTable",
  141. "dynamodb:GetRecords"
  142. ],
  143. "Resource": "*"
  144. }
  145. ]
  146. }
  147. }
  148. },
  149. "HelloWorld": {
  150. "Type": "AWS::Lambda::Function",
  151. "Properties": {
  152. "FunctionName": "HelloWorld",
  153. "Description": "HelloWorld",
  154. "Handler": "index.handler",
  155. "Role": {
  156. "Fn::GetAtt": [
  157. "LambdaExecutionRole",
  158. "Arn"
  159. ]
  160. },
  161. "Code": {
  162. "ZipFile": {
  163. "Fn::Join": [
  164. "\n",
  165. [
  166. "import os,boto3,sys",
  167. "user = os.environ['user']",
  168. "password = os.environ['password']",
  169. "def get_ssm_parameter(parameter_name):",
  170. " try:",
  171. " ssm_client = boto3.client('ssm')",
  172. " response = ssm_client.get_parameters(Names=[parameter_name],WithDecryption=True)",
  173. " if len(response['Parameters']) == 0:",
  174. " print('Error Getting the value for parameter {}.'.format(parameter_name))",
  175. " sys.exit(1)",
  176. " else:",
  177. " value=response['Parameters'][0]['Value']",
  178. " return value",
  179. " except Exception as e:",
  180. " print('Error Getting the value for parameter {}, Error : {}'.format(parameter_name,str(e)))",
  181. " sys.exit(1)",
  182. "def handler(event,context):",
  183. " print(event)",
  184. " db_username = get_ssm_parameter(user)",
  185. " db_password = get_ssm_parameter(password)",
  186. " print('DB User name is {} and password is {}'.format(db_username,db_password))"
  187. ]
  188. ]
  189. }
  190. },
  191. "Environment": {
  192. "Variables": {
  193. "user": {
  194. "Ref": "SSMPanoUser"
  195. },
  196. "password": {
  197. "Ref": "SSMPanoPassword"
  198. }
  199. }
  200. },
  201. "Runtime": "python3.6",
  202. "Timeout": "300"
  203. }
  204. }
  205. },
  206. "Outputs": {
  207. "PanoUser": {
  208. "Value": {
  209. "Ref": "SSMPanoUser"
  210. }
  211. }
  212. }
  213. }
Add Comment
Please, Sign In to add comment