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" />

  <ItemGroup>
    <PackageReference Include="OpenTelekomCloud.Serverless.Function.Common" 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 for all target frameworks defined in the project file and creates a zip file for each target framework in the project folder.

The generated zip files are:

  • sdk_ecs_net6.0.zip

  • sdk_ecs_netcoreapp3.1.zip

  • sdk_ecs_netcoreapp2.1.zip

Deploy the function

Use OpentelekomCloud 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 start_ecs_net6.0.zip from previous step.

Configure function

  • In Configuration > Basic Settings > Handler: set value to name as defined in handler.txt

  • 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.