# Create Lambda function for Lightsail

1. Add function name
2. Select runtime (in our case python)
3. Create the function
4. Add code to stop/start instance:

#### Code to STOP Lightsail Instance:

```python
# TO STOP
import json
import boto3
def lambda_handler(event, context):
    client = boto3.client('lightsail', region_name='REGION-NAME')
    response = client.stop_instance(
    instanceName='INSTANCE-NAME'
)
    return {
        'statusCode': 200,
        'body': json.dumps('Lambda has stopped instance')
    }
```

#### Code to START Lightsail Instance:

```python
# TO START
import json
import boto3
def lambda_handler(event, context):
    client = boto3.client('lightsail', region_name='REGION-NAME')
    response = client.start_instance(
    instanceName='INSTANCE-NAME'
)
    return {
        'statusCode': 200,
        'body': json.dumps('Lambda has started instance')
    }t
```

5\. Go to Amazone EventBridge --> Events --> Rules --> Create Rule

![](/files/G1oiPo9TnRGXG0SV31wY)

6\. Select Scheduled --> Next

![](/files/UpNhH4oR5a0QXHSyP8i5)

7\. Select CRON pattern --> Use <mark style="color:green;">`5 2 ? * 7 *`</mark> for Every Saturday at 2:05 GMT (Example) --> Next

![](/files/VzoWGcWUe92QCnQZw2OK)

8\. On the Right side: <mark style="color:purple;">Select tragets(s) --> AWS Service --> Lambda function --> Select Lambda Function Name</mark>

![](/files/zYeUIZhbdk4Ag5zJHr0u)

9\. Click Next --> Review --> Next --> Finished

### Resources:

[Time Conversion Romania](https://www.worldtimebuddy.com/gmt-to-romania-bucharest)

[AWS Cron Expressions Documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html)


---

# 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://docs.arkannis.net/cloud/aws/lambda/create-lambda-function-for-lightsail.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.
