File size: 3,865 Bytes
2b395f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Lambda Function for FRED ML Analysis'

Parameters:
  FunctionName:
    Type: String
    Default: fred-ml-processor
    Description: Name of the Lambda function
  
  S3BucketName:
    Type: String
    Default: fredmlv1
    Description: S3 bucket for storing reports
  
  Runtime:
    Type: String
    Default: python3.9
    AllowedValues: [python3.8, python3.9, python3.10, python3.11]
    Description: Python runtime version
  
  Timeout:
    Type: Number
    Default: 300
    Description: Lambda function timeout in seconds
  
  MemorySize:
    Type: Number
    Default: 512
    Description: Lambda function memory size in MB

Resources:
  # Lambda Function
  FredMLLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: !Ref FunctionName
      Runtime: !Ref Runtime
      Handler: lambda_function.lambda_handler
      Code:
        ZipFile: |
          import json
          def lambda_handler(event, context):
              return {
                  'statusCode': 200,
                  'body': json.dumps('Hello from Lambda!')
              }
      Timeout: !Ref Timeout
      MemorySize: !Ref MemorySize
      Environment:
        Variables:
          FRED_API_KEY: !Ref FredAPIKey
          S3_BUCKET: !Ref S3BucketName
      Role: !GetAtt LambdaExecutionRole.Arn
      ReservedConcurrencyLimit: 10

  # IAM Role for Lambda
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub '${FunctionName}-execution-role'
      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
      Policies:
        - PolicyName: FredMLLambdaPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:PutObject
                  - s3:DeleteObject
                  - s3:ListBucket
                Resource:
                  - !Sub 'arn:aws:s3:::${S3BucketName}'
                  - !Sub 'arn:aws:s3:::${S3BucketName}/*'
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: '*'

  # CloudWatch Log Group
  LambdaLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub '/aws/lambda/${FunctionName}'
      RetentionInDays: 30

  # SSM Parameter for FRED API Key
  FredAPIKey:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub '/fred-ml/api-key'
      Type: SecureString
      Value: 'your-fred-api-key-here'  # Replace with actual API key
      Description: FRED API Key for Lambda function

  # Lambda Function URL (for direct invocation)
  LambdaFunctionUrl:
    Type: AWS::Lambda::Url
    Properties:
      FunctionName: !Ref FredMLLambdaFunction
      AuthType: NONE
      Cors:
        AllowCredentials: true
        AllowHeaders: ['*']
        AllowMethods: ['GET', 'POST']
        AllowOrigins: ['*']
        MaxAge: 86400

Outputs:
  LambdaFunctionArn:
    Description: ARN of the Lambda function
    Value: !GetAtt FredMLLambdaFunction.Arn
    Export:
      Name: !Sub '${AWS::StackName}-LambdaFunctionArn'
  
  LambdaFunctionUrl:
    Description: URL for direct Lambda function invocation
    Value: !Ref LambdaFunctionUrl
    Export:
      Name: !Sub '${AWS::StackName}-LambdaFunctionUrl'
  
  LambdaExecutionRoleArn:
    Description: ARN of the Lambda execution role
    Value: !GetAtt LambdaExecutionRole.Arn
    Export:
      Name: !Sub '${AWS::StackName}-LambdaExecutionRoleArn'