AWS LambdaにJava11のプログラムをデプロイし、パラメータストアから値を取得するメモ

### パラメータストアに適当なキーを保存
- キー名：testkey
- 値：test-value

![1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/530708/eaad3e6f-0cfe-83bd-d7f5-66f767ed4d28.png)


### ラムダ上に関数をデプロイして実行
![2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/530708/0af6dfb2-917e-5c14-836c-92e720fb198a.png)


### 実行結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/530708/e6835c85-5c1d-bc31-fc9e-e05e8a704040.png)

### ソース作成からデプロイまで
- sam initでテンプレートを作成(AWS Quick Start Templatesを利用)
- sam build(ビルド)
- sam local invoked(ローカル環境で起動テスト)
- sam deploy --guided(AWS Lambdaにデプロイ)

### 修正したソース
#### ①App.java
```java
package helloworld;

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult;

public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Custom-Header", "application/json");

        // パラメータストアに登録しているキーに紐づく値を取得する
        GetParameterRequest req = new GetParameterRequest().withName("testkey").withWithDecryption(true);

        AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();

        GetParameterResult result = ssmclient.getParameter(req);

        String testKey = result.getParameter().getValue();

        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
                .withHeaders(headers);

        String output =
        		String.format("{ \"result\": \"key:%s \"}", testKey);

        return response
                .withStatusCode(200)
                .withBody(output);
    }
}

```

####  ②pom.xmlに追加
```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>helloworld</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>A sample Hello World created for SAM CLI.</name>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-lambda-java-events</artifactId>
          <version>3.6.0</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.1</version>
          <scope>test</scope>
        </dependency>

        <!-- システムマネージャーからパラメータの取得するのに必要 -->
        <dependency>
        	<groupId>com.amazonaws</groupId>
        	<artifactId>aws-java-sdk-ssm</artifactId>
        	<version>1.11.993</version>
        </dependency>

    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.2.4</version>
          <configuration>
          </configuration>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
</project>

```