プログラマーな日々

プログラマーです。AWS認定ソリューションアーキテクト–アソシエイト。好きなものは、C#, TaskChute Cloud, WorkFlowy, Backlog, AWSです。

【AWS】AWS LambdaでData PipelineをActivateする

AWS LambdaでData PipelineをActivateするサンプルです。

PipelineActivator.java

package net.jhashimoto.aws.lambda.datapipelineactivation;

import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.datapipeline.DataPipeline;
import com.amazonaws.services.datapipeline.DataPipelineClientBuilder;
import com.amazonaws.services.datapipeline.model.ActivatePipelineRequest;
import com.amazonaws.services.datapipeline.model.ActivatePipelineResult;

/**
 * パイプラインをActivateする機能を提供します。
 *
 * @author j-hashimoto
 *
 */
public class PipelineActivator {

    /**
    * パイプラインをActivateします。
    * @param pipelineId ActivateするパイプラインのID。
    * @return 実行結果を表すHttpステータスコード。
    */
    public int activate(String pipelineId) {
        DataPipelineClientBuilder builder = DataPipelineClientBuilder.standard();
        DataPipeline client = builder
            .withRegion(Regions.AP_NORTHEAST_1)
            .withCredentials(new EnvironmentVariableCredentialsProvider())    // 環境変数から認証情報を取得
            .build();

        ActivatePipelineRequest request = new ActivatePipelineRequest();
        request.setPipelineId(pipelineId);
        ActivatePipelineResult result = client.activatePipeline(request);
        return result.getSdkHttpMetadata().getHttpStatusCode();
    }
}

AWS LambdaのHandler

package net.jhashimoto.aws.lambda.datapipelineactivation;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, Integer> {

    @Override
    public Integer handleRequest(String pipelineId, Context context) {
        PipelineActivator activator = new PipelineActivator();
        int statusCode = activator.activate(pipelineId);

        return statusCode;
    }
}

JUnitで実行

package net.jhashimoto.aws.lambda.datapipelineactivation;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

import com.amazonaws.services.lambda.runtime.Context;

public class LambdaFunctionHandlerTest {
    @Rule
    public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

    @Before
    public void setEnvironmentVariables() {
        environmentVariables.set("AWS_ACCESS_KEY_ID", "XXXXXXXXXXXXXXXXXXXX");
        environmentVariables.set("AWS_SECRET_ACCESS_KEY", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    }

    private Context createContext() {
        TestContext ctx = new TestContext();

        ctx.setFunctionName("Your Function Name");

        return ctx;
    }

    @Test
    public void testLambdaFunctionHandler() {
        LambdaFunctionHandler handler = new LambdaFunctionHandler();
        Context ctx = createContext();

        int output = handler.handleRequest("df-XXXXXXXXXXXXXXXXXXXX", ctx);
        Assert.assertEquals(200, output);
    }
}