Create Lambda function using AWS CloudShell
Tags: aws, cloudshell, lambda
Published: 2023-09-24 17:17:46

### 1. Access CloudShell from Lambda Console

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg2v06grahgeoyevmmpf.png)
Since npm is already installed in CloudShell, we can use it to create node_modules.

### 2. Create a lambda folder
```bash
mkdir lambda
cd lambda
```

### 3. Open Nano text editor
Run nano index.js to open the nano editing GNU.
```bash
// if no nano installed then run this first
sudo yum install -y nano
// else just run this
nano index.js
```

### 4. Copy the following code into the nano GNU
```javascript
const AWSXRay = require('aws-xray-sdk-core');
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
const s3 = new AWS.S3();
exports.handler = async function (event) {
  return s3.listBuckets().promise();
};
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1usm8xfavsk5o1088rdk.png)

Then `Ctrl-X`, `Y` and `Enter` to exit nano.
We can run the following to double confirm the code has been pasted into index.js
```bash
cat index.js
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q7dbvkxncmnrfin1gjm.png)

### 5. Install the required Dependencies
```bash
npm i aws-xray-sdk
```
Run `ll` to check that node_modules exist and the dependencies are indeed being installed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71f62u1tdromlejd92cv.png)

### 6. Set proper permission for files
```bash
chmod a+r *
```

### 7. Zip the files
```bash
zip -r function.zip .
```
Run `ll` again to double check zipped folder is created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxhzbuemqre1sv295mvw.png)

### 8. Create a IAM role
Since we are using lambda and S3, we need to assign 2 permission policies:

1. AWSLambdaBasicExecutionRole
2. AmazonS3ReadOnlyAccess

Name the IAM role as `DemoLambdaRole`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwoiwzyz4ab0z5v9h6pa.png)

Copy the role ARN
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gioj2ldl2xivn1fx1u9r.png)

### 9. Use cli to upload the zipped folder to create the lambda function
Remember to replace the ARN with yours
```bash
aws lambda create-function --zip-file fileb://function_test.zip --function-name lambda-with-dependencies --runtime nodejs14.x --handler index.handler --role arn:aws:iam::023028579192:role/DemoLambdaRole
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuyl79ql47q6mozvjilb.png)

Refresh the Lambda Functions table and you should see your new lambda being created

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gofd0zmqdt7jrv2vssdp.png)

### 10. Enable x-ray Tracing
Lastly, since we are using x-ray, we also need to enable x-ray tracing at the lambda function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdx142imxzu8cbkfye02.png)

Once you enabled x-ray tracing, go back to the IAM `DemoLambdaRole` and you will see a new permission policy being assigned.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enne46bo4ec4h2wfsphq.png)

### 11. Test the Lambda
You should see all your S3 buckets being returned, as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pxmtk321cxyolw1n94d.png)


### 12. Clean up!
Finally, as a good habit, delete the Lambda to save resources. You may also delete the create Role too.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w9o9wn21p52e9j3kcva.png)

Cheers! [Reference](https://www.udemy.com/course/aws-certified-developer-associate-dva-c01/)