notes on AWS Lambda
This commit is contained in:
		
							parent
							
								
									05b487c3b0
								
							
						
					
					
						commit
						fe0528be0e
					
				
					 7 changed files with 132 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -25,7 +25,7 @@ Beneath this we have a code editor with the handler function with a basic boiler
 | 
			
		|||
 | 
			
		||||
Next we need to add a [trigger](/DevOps/AWS/AWS_Lambda/Lambda_triggers.md) that execute the handler.
 | 
			
		||||
 | 
			
		||||
We will do this using [AWS API Gateway](/DevOps/AWS/AWS_Api_Gateway.md). We select "Add trigger" from the dashboard view and input basic settings:
 | 
			
		||||
We will do this using [AWS API Gateway](/DevOps/AWS/AWS_API_Gateway.md). We select "Add trigger" from the dashboard view and input basic settings:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										115
									
								
								DevOps/AWS/AWS_SAM/AWS_SAM.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								DevOps/AWS/AWS_SAM/AWS_SAM.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,115 @@
 | 
			
		|||
---
 | 
			
		||||
categories:
 | 
			
		||||
  - DevOps
 | 
			
		||||
  - Backend
 | 
			
		||||
tags: [AWS]
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
# AWS SAM
 | 
			
		||||
 | 
			
		||||
SAM stands for **serverless application model**. It is a framework developed by AWS to simplify the process of building, deploying and managing serverless applications. It provides a concise syntax for defining the components of a serverless application, such as [Lambda functions](/DevOps/AWS/AWS_Lambda/Lambda_programming_model.md), [API gateway](/DevOps/AWS/AWS_API_Gateway.md) and database tables.
 | 
			
		||||
 | 
			
		||||
The SAM infrastructure is defined in a YAML file which is then deployed to AWS. SAM syntax gets transformed into CloudFormation during the deployment process. (CloudFormation is a broader and more robust AWS tool for large, highly scaleable infrastructures).
 | 
			
		||||
 | 
			
		||||
## Key features of SAM
 | 
			
		||||
 | 
			
		||||
- Single deployment configuration
 | 
			
		||||
- Integration with development tools
 | 
			
		||||
- Local testing and debugging
 | 
			
		||||
- Built on AWS CloudFormation
 | 
			
		||||
 | 
			
		||||
## Main technologies required
 | 
			
		||||
 | 
			
		||||
### Docker
 | 
			
		||||
 | 
			
		||||
Whilst SAM can be used to create a deployable file for AWS it can also be run as a container for local development with Docker.
 | 
			
		||||
 | 
			
		||||
### AWS CLI
 | 
			
		||||
 | 
			
		||||
This is installed using Python and allows you to interact directly with AWS via the command-line.
 | 
			
		||||
 | 
			
		||||
### AWS SAM CLI
 | 
			
		||||
 | 
			
		||||
See [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
 | 
			
		||||
 | 
			
		||||
## Setting up credentials for the AWS CLI
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
aws configure
 | 
			
		||||
AWS Access Key ID [None]: AK*******
 | 
			
		||||
AWS Secret Access Key [None]: ukp******
 | 
			
		||||
Default region name [None]:
 | 
			
		||||
Default output format [None]:
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This information can be found in the Security Credentials section of the given [IAM](/DevOps/AWS/AWS_User_management_and_roles.md#iam) user:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
## Starting a SAM project
 | 
			
		||||
 | 
			
		||||
First create a directory for your project which will serve as the repository:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
mkdir aws-sam-learning
 | 
			
		||||
cd aws-sam-learning
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Then we can use the `sam` cli to bootstrap the project:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
sam init --runtime nodejs16.x
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
We can just click through and accept the basic HelloWorld Lambda.
 | 
			
		||||
 | 
			
		||||
This will create the Lambda as well as an API Gateway trigger URL.
 | 
			
		||||
 | 
			
		||||
### `template.yaml`
 | 
			
		||||
 | 
			
		||||
This is autogenerated and details the main constituents of the project. There are lots of fields but the most important are the following:
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
HelloWorldFunction:
 | 
			
		||||
  Type: AWS::Serverless::Function
 | 
			
		||||
  Properties:
 | 
			
		||||
    CodeUri: hello-world/
 | 
			
		||||
    Handler: app.lambdaHandler
 | 
			
		||||
    Runtime: nodejs16.x
 | 
			
		||||
    Architectures:
 | 
			
		||||
      - x86_64
 | 
			
		||||
    Events:
 | 
			
		||||
      HelloWorld:
 | 
			
		||||
        Type: Api
 | 
			
		||||
        Properties:
 | 
			
		||||
          Path: /hello
 | 
			
		||||
          Method: get
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This details the location of the [handler function](/DevOps/AWS/AWS_Lambda/Lambda_handler_function.md) which is contained at the path `hello-world/app.js`:
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
exports.lambdaHandler = async (event, context) => {
 | 
			
		||||
  try {
 | 
			
		||||
    // const ret = await axios(url);
 | 
			
		||||
    response = {
 | 
			
		||||
      statusCode: 200,
 | 
			
		||||
      body: JSON.stringify({
 | 
			
		||||
        message: "hello world",
 | 
			
		||||
        // location: ret.data.trim()
 | 
			
		||||
      }),
 | 
			
		||||
    };
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.log(err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return response;
 | 
			
		||||
};
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
It also lists the `get` event that we can use to call API Gateway and trigger the Lambda.
 | 
			
		||||
 | 
			
		||||
The full template is below:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
							
								
								
									
										15
									
								
								DevOps/AWS/Developing_AWS_projects_locally.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								DevOps/AWS/Developing_AWS_projects_locally.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,15 @@
 | 
			
		|||
---
 | 
			
		||||
categories:
 | 
			
		||||
  - DevOps
 | 
			
		||||
tags: [AWS]
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
# Developing AWS projects locally
 | 
			
		||||
 | 
			
		||||
To develop AWS projects locally the following core tools can be used to set up a local development environment that simulates AWS services:
 | 
			
		||||
 | 
			
		||||
- AWS CLI
 | 
			
		||||
  - Interact with AWS services from your local machine
 | 
			
		||||
- AWS SDK
 | 
			
		||||
  - Integrate AWS services into your given programming language and application code
 | 
			
		||||
- [AWS SAM](/DevOps/AWS/AWS_SAM/AWS_SAM.md)
 | 
			
		||||
| 
						 | 
				
			
			@ -6,7 +6,7 @@ tags: [physics, electricity]
 | 
			
		|||
 | 
			
		||||
# Cells and batteries
 | 
			
		||||
 | 
			
		||||
Cells are a [voltage source](/Electronics_and_Hardware/Analogue_circuits/Voltage.md#chemicals-cells-and-batteries) that generate a difference of potential via a positive and negative electrode separated by an electrolytic solution. The electrolytes pull free electrons from one of the materials which creates a positive charge. The other material gains the free electrons creating a negative charge.
 | 
			
		||||
Cells are a [voltage source](/Electronics_and_Hardware/Analogue_circuits/Voltage.md) that generate a difference of potential via a positive and negative electrode separated by an electrolytic solution. The electrolytes pull free electrons from one of the materials which creates a positive charge. The other material gains the free electrons creating a negative charge.
 | 
			
		||||
 | 
			
		||||
> A battery is a combination of two or more cells.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										
											BIN
										
									
								
								_img/access-key-aws.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								_img/access-key-aws.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 20 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								_img/sam-template-yaml.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								_img/sam-template-yaml.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 194 KiB  | 
		Loading…
	
	Add table
		
		Reference in a new issue