# Lambdaをローカルでテスト(with Docker)

## Lambdaをローカルでテスト(with Docker)

* Dockerfile

```
FROM lambci/lambda:build-python3.6
ENV AWS_DEFAULT_REGION ap-northeast-1
 
ADD . .
 
CMD pip3 install -r requirements.txt -t /var/task && \
  zip -9 deploy_package.zip lambda_function.py && \
  zip -r9 deploy_package.zip *
```

* requirements.txt

```
paramiko == 2.7.1
cffi == 1.14.1
boto3 == 1.14.35
pycrypto == 2.6.1
```

* build.sh

```
ls |  grep -v -E 'lambda_function.py|sample.py|requirements.txt|Dockerfile|build.sh' | xargs rm -Rf
docker build -t sshmonitor .
docker run -v "$PWD":/var/task sshmonitor 
```

* lambda\_function.py

```
import boto3
import logging
import paramiko

print('Start Scanning EC2 instances which password authentication is set to')

logging.basicConfig()
logging.getLogger("paramiko").setLevel(logging.DEBUG)

def lambda_handler(event, context):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ec2 = boto3.client('ec2')
    response = ec2.describe_instances()
    reservations = response["Reservations"]

    for reservation in reservations:
        instance = reservation["Instances"][0]

        try:
            ip = instance["PublicIpAddress"]
            print('Accessing: ' + ip)
            client.connect(ip, username='Security Scanner From AWS Hiring System Please Ignore', password='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
        except:
            print('Done: ' + ip)

    return 'Finish Scanning!'
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hayashier.gitbook.io/article/aws/test-lambda-locally-with-docker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
