# 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FNEZFIxQWe10O6qf0SV9w%2Fimage.png?alt=media\&token=4476ffa7-42ff-45e4-8d42-9e86b35829cb)

6\. Select Scheduled --> Next

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FeYubadE22vfiOG9KoAJD%2Fimage.png?alt=media\&token=682783ca-f2f8-49f1-afe7-74104965c9e6)

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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F3nDRCLvw6HMNbYDOQPpj%2Fimage.png?alt=media\&token=5142118b-21d5-41c7-b462-fdd19cb8998d)

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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FdZKiog8SIoBfmkSA3XlS%2Fimage.png?alt=media\&token=e253e823-e513-4ad9-8cf3-b241a5dd4027)

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)
