in functions/source/onboarding/onboarding.py [0:0]
def create(event, context):
logger.info(json.dumps(event))
try:
firstLaunch = False
stackSetName = os.environ['stackSetName']
stackSetUrl = os.environ['stackSetUrl']
newRelicAccId = os.environ['newRelicAccId']
newRelicSecret = os.environ['newRelicSecret']
newRelicStackSNS = os.environ['newRelicStackSNS']
managementAccountId = context.invoked_function_arn.split(":")[4]
cloudFormationClient = session.client('cloudformation')
regionName = context.invoked_function_arn.split(":")[3]
cloudFormationClient.describe_stack_set(StackSetName=stackSetName)
logger.info('Stack set {} already exist'.format(stackSetName))
helper.Data.update({"result": stackSetName})
except Exception as describeException:
logger.info('Stack set {} does not exist, creating it now.'.format(stackSetName))
cloudFormationClient.create_stack_set(
StackSetName=stackSetName,
Description='Adds in New Relic integration to your aws accounts. Launch as Stack Set in your Control Tower landing zone management account.',
TemplateURL=stackSetUrl,
Parameters=[
{
'ParameterKey': 'NewRelicAccountNumber',
'ParameterValue': newRelicAccId,
'UsePreviousValue': False,
'ResolvedValue': 'string'
}
],
Capabilities=[
'CAPABILITY_NAMED_IAM'
],
AdministrationRoleARN='arn:aws:iam::' + managementAccountId + ':role/service-role/AWSControlTowerStackSetRole',
ExecutionRoleName='AWSControlTowerExecution')
try:
result = cloudFormationClient.describe_stack_set(StackSetName=stackSetName)
firstLaunch = True
logger.info('StackSet {} deployed'.format(stackSetName))
except cloudFormationClient.exceptions.StackSetNotFoundException as describeException:
logger.error('Exception getting new stack set, {}'.format(describeException))
raise describeException
try:
if firstLaunch and len(os.environ['seedAccounts']) > 0 :
logger.info("New accounts : {}".format(os.environ['seedAccounts']))
accountList = os.environ['seedAccounts'].split(",")
snsClient = session.client('sns')
messageBody = {}
messageBody[stackSetName] = { 'target_accounts': accountList, 'target_regions': [regionName] }
try:
snsResponse = snsClient.publish(
TopicArn=newRelicStackSNS,
Message = json.dumps(messageBody))
logger.info("Queued for stackset instance creation: {}".format(snsResponse))
except Exception as snsException:
logger.error("Failed to send queue for stackset instance creation: {}".format(snsException))
else:
logger.info("No additional StackSet instances requested")
except Exception as create_exception:
logger.error('Exception creating stack instance with {}'.format(create_exception))
raise create_exception
helper.Data.update({"result": stackSetName})
# To return an error to cloudformation you raise an exception:
if not helper.Data.get("result"):
raise ValueError("Error occured during solution onboarding")
return None #Generate random ID