ECS Sample to start/stop/reboot an instance

This sample demonstrates how to start/stop/reboot an ECS instance using FunctionGraph and:

Prerequisites

  • For this example an ECS instance must exist.

  • The function must have permissions to start/stop/reboot the ECS instance.

    This can be achieved by creating an agency with a policy granting the permission ecs:*:start, ecs:*:stop, and ecs:*:reboot and specifying this agency when creating the function. (E.g. create an agency with ECS User System-defined policy).

Source

Source for this sample can be found in: /samples-doc/sdk-ecs.

/sdk_ecs.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <Import Project="..\..\common.props" />

   <PropertyGroup> 
    <TargetFrameworks>net6.0</TargetFrameworks>
   </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="OpenTelekomCloud.Serverless.Function.Common" Version="*-*" />
    <PackageReference Include="OpenTelekomCloud.Serverless.Function.Common.JsonSerializer"
      Version="*-*" />
    <PackageReference Include="OpenTelekomCloud.Serverless.Function.Events.Timer" Version="*-*" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="OpenTelekomCloud.API.Signing.Core" Version="*-*" />
  </ItemGroup>

</Project>

Build the project

To build the project, navigate to the project directory and run the following command:

dotnet build -c Release

This command builds the project and creates a zip file in the src folder, named sdk_ecs_net6.0.zip

Deploy the function

Use T Cloud Public FunctionGraph console to create a function with following settings:

Create function

Create With: Create from scratch

Basic Information

  • Function Type Event Function

  • Region <YOUR REGION>

  • Function Name <YOUR FUNCTION NAME>

  • Agency Specify an agency with policy to start ECS instance

  • Runtime C# (.NET 6.0)

Upload code

Use Upload > Local ZIP and upload sdk_ecs_net6.0.zip from previous step.

Configure function

  • In Configuration > Basic Settings > Handler: set value to name as defined in handler.txt (either sdk_ecs::src.Program::HandlerECS or sdk_ecs::src.ProgramToken::HandlerECS)

  • In Configuration > Environment Variables add following variables:

    Environment variables

    Environment variable name

    Value

    Remarks

    ECS_INSTANCE_ID

    <ID of ecs instance>

    ID of ECS instance to start

    ECS_ENDPOINT_URL

    <ecs endpoint>

    Default: https://ecs.eu-de.otc.t-systems.com see Regions and Endpoints

    ECS_ACTION

    <action>

    Action to perform on the ECS instance (“start”, “stop”, “reboot”), default: “start”

    ECS_ACTION_TYPE

    <action type>

    Action type to perform on the ECS instance for reboot/stop (“SOFT”, “HARD”), default: “SOFT”

Test the function

Create Test Event

In Code create a Test Event using “Blank Template” (Event is not used in function).

Test function

Click Test to test function.

The function execution result is displayed in the Execution Result section.

Trigger usage

You can create triggers to start/stop the ECS instance at a specific time.

In Configuration > Triggers

  • Click Create Trigger to create stop trigger to stop instance every day at 17:00:

    • Trigger Type: Timer

    • Timer Name: stop

    • Rule: Cron expression, Value: CRON_TZ=Europe/Berlin 0 0 17 * * *

    • Enable Trigger enable

    • Additional Information: stop

  • Click Create Trigger to create start trigger to start instance every day at 08:00:

    • Trigger Type: Timer

    • Timer Name: start

    • Rule: Cron expression, Value: CRON_TZ=Europe/Berlin 0 0 8 * * *

    • Enable Trigger enable

    • Additional Information: start