So you have used Spring Cloud SSM access for elastic beanstalk and docker but want to get into lambda with same nice config setup.
Sadly Spring framework is a bit too heavy for lambda and they suggest Dagger 2 or Guice. This guide is not about static/dynamic wiring of beans together but on getting parameters into your beans.
In the old days you had to place all of your environment path or via a file in s3 which you had to parse. Now this was ok for simple things but it was not secure for secrets aka database passwords or other sub-systems external to aws.
So most people rolled their own kms decryption system when it loads in lambda, ok that's nice but its still not easy to test locally vs on the cloud.
This code was inspired by the spring-cloud-aws project. But without the spring bits. (yep this does not do the nice overlays with dynamic profile activation etc but its still a good steps away from having all of your properties on the environment path)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Config { | |
private SsmParamUtil ssmParamUtil; | |
public void init() { | |
//read environment and setup property collector | |
ssmParamUtil = new SsmParamUtil(prefix, AWSSimpleSystemsManagementClientBuilder.defaultClient()); | |
} | |
public String getValue(String key) { | |
if(ssmParamUtil != null) { | |
String ssmValue = (String)ssmParamUtil.getProperty(key); | |
if (!StringUtils.isNullOrEmpty(ssmValue)){ | |
return ssmValue; | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; | |
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathRequest; | |
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult; | |
import com.amazonaws.services.simplesystemsmanagement.model.Parameter; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Set; | |
public class SsmParamUtil { | |
private String context; | |
private Map<String, Object> properties = new LinkedHashMap<>(); | |
private AWSSimpleSystemsManagement ssmClient; | |
public SsmParamUtil(String context, AWSSimpleSystemsManagement ssmClient) { | |
this.context = context; | |
this.ssmClient = ssmClient; | |
} | |
public void init() { | |
GetParametersByPathRequest paramsRequest = new GetParametersByPathRequest() | |
.withPath(context).withRecursive(true).withWithDecryption(true); | |
getParameters(paramsRequest); | |
} | |
public String[] getPropertyNames() { | |
Set<String> strings = properties.keySet(); | |
return strings.toArray(new String[strings.size()]); | |
} | |
public Object getProperty(String name) { | |
return properties.get(name); | |
} | |
private void getParameters(GetParametersByPathRequest paramsRequest) { | |
GetParametersByPathResult paramsResult = ssmClient | |
.getParametersByPath(paramsRequest); | |
for (Parameter parameter : paramsResult.getParameters()) { | |
String key = parameter.getName().replace(context, "").replace('/', '.'); | |
properties.put(key, parameter.getValue()); | |
} | |
if (paramsResult.getNextToken() != null) { | |
getParameters(paramsRequest.withNextToken(paramsResult.getNextToken())); | |
} | |
} | |
} |
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Ref LambdaRoleName
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: '2012-10-17'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName:
Fn::Join:
- '-'
- - Ref: Product
- Application-Lambda-Policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:DescribeParameters
Resource: "*"
- Effect: Allow
Action:
- ssm:GetParameters
- ssm:GetParameter
- ssm:GetParametersByPath
Resource:
- !Sub "arn:aws:ssm:*:*:parameter/config/${Service}"
- !Sub "arn:aws:ssm:*:*:parameter/config/${Service}/*"
- !Sub "arn:aws:ssm:*:*:parameter/config/${Service}_*"
- !Sub "arn:aws:ssm:*:*:parameter/config/${Service}_*/*"
- Effect: Allow
Action:
- kms:Decrypt
Resource:
- Ref: SSMKey
No comments:
Post a Comment