Why my Lightsail Instance always turn off?

0

I am new using AWS Lightsail so I try to create one Instance for my wordpress site. But I don't know why, the server always need to reboot because the service is always unavailable. Anyone have the same problem?

asked 2 years ago219 views
2 Answers
0

Hi, so i see you say your lightsail instance reboots itself i assume automatically.

So you may want to schedule regular reboots or stop/starts on your Lightsail instances. This provides steps to automate this like EC2 Scheduler does with EC2 instances.

1.  Create an IAM policy and execution role for your Lambda function.

First you have to create an IAM policy using the JSON policy editor, copy and paste the following JSON policy document into the policy editor:


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "RebootLighsail",
            "Effect": "Allow",
            "Action": [
                "lightsail:RebootInstance",
                "logs:CreateLogStream",
                "logs:CreateLogGroup",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

Next, create an IAM role for Lambda and select the policy you just created when attaching the permissions.

2.  Create a Lambda function that will reboot your instance.

In the AWS Lambda console, choose 'Create function'. Then choose 'Author from scratch' and under 'Basic information', add the following:

   For Function name, enter a name, for example, "RebootInstances".
   For Runtime, choose Python 3.9.
   Under Permissions, expand Change default execution role.
   Under Execution role, choose Use an existing role.
   Under Existing role, choose the IAM role that you created for Lambda.

>Now choose Create function. Under Code, Code source, copy and paste the following code into the editor pane in the code editor. This code will reboot the instance you specify:

import json
import boto3
def lambda_handler(event, context):
    client = boto3.client('lightsail', region_name='us-east-1')
    response = client.reboot_instance(
    instanceName='Test'
)
    return {
        'statusCode': 200,
        'body': json.dumps('Instance rebooted.')
    }


^ Replace the region_name (us-east-1) with the region of the instance and the name (Test) with the instance name.

Now choose Deploy. On the Configuration tab, choose General configuration, Edit. Set Timeout to 10 seconds and then select Save.

3. Test your Lambda function.

In the AWS Lambda console, choose Functions and then select the function that you created.

Select the Code tab and in the Code source section, select Test. In the Configure test event dialog box, choose Create new test event and enter an Event name. Then, choose Create.

(Note: You don't need to change the JSON code for the test event since the function doesn't use it.)

Choose Test to run the function. You should see now that your instance has been rebooted.

4. Create EventBridge rules that trigger your Lambda function.

This is the part where you create a rule that will run your Lambda function on a schedule, for example once a day or once a week.

Open the Eventbridge console and select Create rule.

Enter a Name for your rule, such as "RebootLightsailInstances". You can enter a Description if you want.

In Define pattern, select Schedule and do either of the following, this will be how often and when the instance is rebooted:





For Fixed rate of, enter an interval of time in minutes, hours, or days.
For Cron expression, enter an expression that tells Lambda when to reboot your instances.
(Note: Cron expressions are evaluated in UTC. Make sure that you adjust the expression for your preferred time zone.)

In Select targets, choose Lambda function from the Target drop-down menu and then choose the function that you created earlier.

Scroll down and then select Create.


Once you complete the steps above, your Lightsail instance will be rebooted regularly on schedule as per your EventBridge rule.

If you want to schedule stop/start, you can create 2 Lambda functions and Eventbridge rules. Use the following code for the functions:

Stop instance code:

import json
import boto3

def lambda_handler(event, context):
    client = boto3.client('lightsail', region_name='us-east-1')
    response = client.stop_instance(
    instanceName='Test'
)
    return {
        'statusCode': 200,
        'body': json.dumps('Instance stopped.')
    }

Start instance code:

import json
import boto3
def lambda_handler(event, context):
    client = boto3.client('lightsail', region_name='us-east-1')
    response = client.start_instance(
    instanceName='Test'
)
    return {
        'statusCode': 200,
        'body': json.dumps('Instance started successfully.')
    }
answered 2 years ago
0

Hi there There’s a few things you can check to ensure your lightsail server is functioning well:

  1. Check the lighstail instance metrics i.e. CPUUtilization and BurstCapacityPercentage -CPUUtilization ensure that it is mostly within CPU utilization sustainable -BurstCapacityPercentage ensure there is enough capacity available and it’s not flat lining on 0
  1. Check the system log files /var/log/messages or /var/log/syslog and ensure there are no “out of memory” errors reported
  1. Ensure that the system disk space is not filled up (at the time of the issue)

[1]https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-viewing-instance-health-metrics#cpu-utilization-zones

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions