AWS Lambda Create a Lambda Function with CloudFromation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 16: Line 16:


  Resources:
  Resources:
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: playground-lambda-execution-role
      Path: /service-role/
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Principal:
              Service:
                - "lambda.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Policies:
        - PolicyName: 'inline-policy'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: 'Allow'
                  Action:
                    - 'logs:CreateLogGroup'
                    - 'logs:CreateLogStream'
                    - 'logs:PutLogEvents'
                  Resource:
                    - 'arn:aws:logs:*:*:*'
                - Effect: 'Allow'
                Action:
                  - 'ec2:CreateNetworkInterface'
                  - 'ec2:DescribeNetworkInterfaces'
                  - 'ec2:DeleteNetworkInterface'
                Resource:
                  - '*'
   LambdaExample:
   LambdaExample:
     Type: "AWS::Lambda::Function"
     Type: AWS::Lambda::Function
     Properties:  
     Properties:  
       [[AWS_Lambda_Concepts#Function_Name|FunctionName]]: my-lambda
       [[AWS_Lambda_Concepts#Function_Name|FunctionName]]: lambda-experiment
       Description: 'Some description'
       Description: 'Some description'
       [[AWS_Lambda_Concepts#Code|Code]]:
       [[AWS_Lambda_Concepts#Code|Code]]:
         S3Bucket: a-bucket
         S3Bucket: a-bucket
         S3Key: a-deployment-package
         S3Key: a-deployment-package.jar
         # S3ObjectVersion: String
         # S3ObjectVersion: String
         # ZipFile: String
         # ZipFile: String
       [[AWS_Lambda_Concepts#Handler_Code|Handler]]: handler
       [[AWS_Lambda_Concepts#Handler_Code|Handler]]: playground.amazon.lambda.LambdaExperiment::handleRequest
       [[AWS_Lambda_Concepts#Runtime|Runtime]]: java-1.8.0-openjdk
       [[AWS_Lambda_Concepts#Runtime|Runtime]]: java8
       [[AWS_Lambda_Concepts#Environment_Variables|Environment]]:
       [[AWS_Lambda_Concepts#Environment_Variables|Environment]]:
         <span id='Variables'></span>Variables:
         <span id='Variables'></span>Variables:
Line 33: Line 66:
       [[AWS_Lambda_Concepts#Relationship_to_a_VPC|VpcConfig]]:
       [[AWS_Lambda_Concepts#Relationship_to_a_VPC|VpcConfig]]:
         SubnetIds:
         SubnetIds:
           - red-subnet
           - !Ref BlueSubnet
          - !Ref RedSubnet
         SecurityGroupIds:
         SecurityGroupIds:
           - ...
           - ...
       [[AWS_Lambda_Concepts#Memory_Configuration|MemorySize]]: 128
       [[AWS_Lambda_Concepts#Memory_Configuration|MemorySize]]: 128
       [[AWS_Lambda_Concepts#Role|Role]]: arn:aws:iam::77777777777:role/service-role/my-function-execution-role
       [[AWS_Lambda_Concepts#Role|Role]]: !GetAtt LambdaExecutionRole.Arn
       [[AWS_Lambda_Concepts#Timeout|Timeout]]: ''Integer''
       [[AWS_Lambda_Concepts#Timeout|Timeout]]: ''Integer''
       [[AWS_Lambda_Concepts#Concurrent_Execution|ReservedConcurrentExecutions]]: ''Integer''
       [[AWS_Lambda_Concepts#Concurrent_Execution|ReservedConcurrentExecutions]]: ''Integer''
Line 49: Line 83:
       Tags:
       Tags:
         Resource Tag
         Resource Tag
Working example:
<syntaxhighlight lang='yaml'>
</syntaxhighlight>
=CloudFormation Stack Example=
{{External|https://github.com/ovidiuf/aws-release-pipeline/blob/master/lambda/lambda.yaml}}


=Create a bash Lambda=
=Create a bash Lambda=
Line 55: Line 99:


=Create a Java Lambda=
=Create a Java Lambda=
{{External|[https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model.html Building Lambda Functions with Java]}}
{{Internal|AWS Java Lambda Development|AWS Java Lambda Development}}

Latest revision as of 03:41, 8 April 2019

External

Internal

Resource Types

AWS::Lambda::Function

AWS::Lambda::Function
CreateFunction
Resources:
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: playground-lambda-execution-role
      Path: /service-role/
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Principal:
              Service:
                - "lambda.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Policies:
        - PolicyName: 'inline-policy'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: 'Allow'
                 Action:
                   - 'logs:CreateLogGroup'
                   - 'logs:CreateLogStream'
                   - 'logs:PutLogEvents'
                 Resource:
                   - 'arn:aws:logs:*:*:*'
               - Effect: 'Allow'
                Action:
                  - 'ec2:CreateNetworkInterface'
                  - 'ec2:DescribeNetworkInterfaces'
                  - 'ec2:DeleteNetworkInterface'
                Resource:
                  - '*'
  LambdaExample:
    Type: AWS::Lambda::Function
    Properties: 
      FunctionName: lambda-experiment
      Description: 'Some description'
      Code:
        S3Bucket: a-bucket
        S3Key: a-deployment-package.jar
        # S3ObjectVersion: String
        # ZipFile: String
      Handler: playground.amazon.lambda.LambdaExperiment::handleRequest
      Runtime: java8
      Environment:
        Variables:
          MY_ENV_VAR: 'my value'
      VpcConfig:
        SubnetIds:
          - !Ref BlueSubnet
          - !Ref RedSubnet
        SecurityGroupIds:
          - ...
      MemorySize: 128
      Role: !GetAtt LambdaExecutionRole.Arn
      Timeout: Integer
      ReservedConcurrentExecutions: Integer
      DeadLetterConfig:
        DeadLetterConfig
      KmsKeyArn: String
      Layers:
        - String
      TracingConfig:
        TracingConfig
      Tags:
        Resource Tag


Working example:

CloudFormation Stack Example

https://github.com/ovidiuf/aws-release-pipeline/blob/master/lambda/lambda.yaml

Create a bash Lambda

Publishing a Custom Runtime

Create a Java Lambda

AWS Java Lambda Development